Facebook SDK IOS xcode

5
大家好,我正在使用Xcode开发iOS应用程序。我想实现Facebook登录并获取用户名和Facebook ID,并将其发送到WebView链接中,这是否可行?
第一个问题是,即使按照此链接中的Facebook指南进行操作: https://developers.facebook.com/docs/ios/getting-started,我仍然无法让Facebook登录运行。iOS模拟器启动,但最终出现以下输出信息:
2015-04-21 19:51:20.069 DONAT[3899:96245] +[FBSDKButton defaultIcon]: unrecognized selector sent to class 0x103800908
2015-04-21 19:51:20.073 DONAT[3899:96245] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[FBSDKButton defaultIcon]: unrecognized selector sent to class 0x103800908'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001040e9f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000103d82bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001040f0f4d +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010404927c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000104048e18 _CF_forwarding_prep_0 + 120
    5   DONAT                               0x00000001037b6207 -[FBSDKButton configureButton] + 47
    6   DONAT                               0x00000001037b5509 -[FBSDKButton awakeFromNib] + 73
    7   UIKit                               0x00000001049dee75 -[UINib instantiateWithOwner:options:] + 2010
    8   UIKit                               0x000000010483df98 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
    9   UIKit                               0x000000010483e588 -[UIViewController loadView] + 109
    10  UIKit                               0x000000010483e7f9 -[UIViewController loadViewIfRequired] + 75
    11  UIKit                               0x000000010483ec8e -[UIViewController view] + 27
    12  UIKit                               0x000000010475dca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
    13  UIKit                               0x000000010475e041 -[UIWindow _setHidden:forced:] + 247
    14  UIKit                               0x000000010476a72c -[UIWindow makeKeyAndVisible] + 42
    15  UIKit                               0x0000000104715061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
    16  UIKit                               0x0000000104717d2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    17  UIKit                               0x0000000104716bf2 -[UIApplication workspaceDidEndTransaction:] + 179
    18  FrontBoardServices                  0x0000000107bc42a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    19  CoreFoundation                      0x000000010401f53c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    20  CoreFoundation                      0x0000000104015285 __CFRunLoopDoBlocks + 341
    21  CoreFoundation                      0x0000000104015045 __CFRunLoopRun + 2389
    22  CoreFoundation                      0x0000000104014486 CFRunLoopRunSpecific + 470
    23  UIKit                               0x0000000104716669 -[UIApplication _run] + 413
    24  UIKit                               0x0000000104719420 UIApplicationMain + 1282
    25  DONAT                               0x000000010378d743 main + 115
    26  libdyld.dylib                       0x00000001068bc145 start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我的应用程序委托(app delegate.h)文件。
#import <UIKit/UIKit.h>



@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

这是我的AppDelegate.m文件。
#import "AppDelegate.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>




@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [FBSDKLoginButton class];
    return YES;
} //AGGIUNTO DA FB DEVELPER

- (void)applicationWillResignActive:(UIApplication *)application {

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp]; //AGGIUNTO DA FACEBOOK DEVELOPER GUIDE
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}//AGGIUNTO DA fbdeveloper


@end

在我的Main.storyboard中,我有一个名为Login的视图控制器,它由相应的Login.h和Login.m文件组成,并且该视图使用FBSDKButton类告诉Facebook。

编辑

好的,登录可以工作了,但我无法在登录后显示另一个视图控制器。

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {

    NSString * storyboardName = @"dashBoardViewController";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"dashBoardViewController"];
    [self presentViewController:vc animated:YES completion:nil];
}

这是我现在的方法,视图控制器已经有一个storyboard id = dashBoardViewController,但它仍然无法工作。

请记住,您的模拟器可能没有 Facebook 帐户,因此可能会崩溃。您应该在没有安装和工作的 Facebook 的设备上尝试这个。 - sam_smith
FBLoginView来自过时的Facebook iOS SDK版本。你能否升级到iOS SDK 4.0.1并在此分享任何错误?第二个问题似乎不是由Facebook引起的。 - curveorzos
3个回答

14

这可能不是完全符合你的问题,但至少与之相关。虽然Facebook的指示说要将按钮的类设置为FBSDKButton,但这样做会像你上面描述的那样崩溃。将按钮的类设置为FBSDKLoginButton似乎可以解决问题。


这应该是针对特定问题的最佳答案 - FBSDKButton 崩溃但 FBSDKLoginButton 可以正常工作。非常感谢! - Julien Perrenoud

7
我遇到了完全相同的错误。
经过检查,我发现我的项目需要使用:FBSDKCoreKit、FBSDKLoginKit和FBSDKShareKit。
如果你使用cocoapods,这是在Podfile中的一行代码:
pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
pod 'FBSDKShareKit'

在执行 pod install 后,将以下内容添加到您的桥接头文件(.h 文件)中:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

在你的故事板中,使用类类型FBSDKLoginButton定义一个视图:

enter image description here


0
我曾经遇到过同样的问题。您需要在视图控制器中导入FBSDKLoginKit,然后使用FBSDKLoginButton。以下是代码片段:
import FBSDKLoginKit
class ViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
   let fbButton = FBSDKLoginButton()
    fbButton.center = self.view.center
    self.view.addSubview(fbButton)
    // Do any additional setup after loading the view, typically from a nib.
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接