简体   繁体   English

Swift / Backendless-Facebook Login在iPhone上有效,但在iPad Simulator上无效。 如何正确使用Facebook SDK?

[英]Swift / Backendless - Facebook Login working on iPhone, but not on iPad Simulator. How to properly use Facebook SDK?

I'm using the following function to use the FacebookSDK Login for my backendless users: 我正在使用以下功能为无后端用户使用FacebookSDK登录:

func application(application: UIApplication,
                 openURL url: NSURL,
                 sourceApplication: String?,
                 annotation: AnyObject) -> Bool {

    let result = FBSDKApplicationDelegate.sharedInstance().application(application,
                                                                       openURL: url,
                                                                       sourceApplication: sourceApplication,
                                                                       annotation: annotation)
    if result {

        let token = FBSDKAccessToken.currentAccessToken()
        let fieldsMapping = [
            "id" : "facebookId",
            "name" : "name",
            "birthday": "birthday",
            "first_name": "fb_first_name",
            "last_name" : "fb_last_name",
            "gender": "gender",
            "email": "email"
        ]

        // REQUEST FB IMAGE
        let request = FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"email"], tokenString: token.tokenString, version: nil, HTTPMethod: "GET")

        request.startWithCompletionHandler({ (connection, result, error : NSError!) in

            if error == nil {
                let facebookID = result["id"]! as! String
                let avatarURL = "https://graph.facebook.com/\(facebookID)/picture?type=normal"

                updateBackendlessUser(facebookID, avatarURL: avatarURL)
            } else {
                print(error)
            }
        })
        // REQUEST FB IMAGE END

        backendless.userService.loginWithFacebookSDK(
            token,
            fieldsMapping: fieldsMapping,
            response: { (user: BackendlessUser!) -> Void in
               // self.backendless.userService.currentUser = user
                print("user: \(user)")

                dispatch_async(dispatch_get_main_queue()) {
                    let lcVC = stb.instantiateViewControllerWithIdentifier("LoadContentVC")
                    self.window?.rootViewController = lcVC
                    self.window?.makeKeyAndVisible()
                }

            },
            error: { (fault: Fault!) -> Void in
                print("Server reported an error: \(fault)")
        })
    }      
    return result
}

This is usally working fine. 通常情况下工作正常。 Today I wanted to check my interface on an iPad Air2 Simulator. 今天,我想检查一下iPad Air2 Simulator上的界面。 The login resulted in fatal error: unexpectedly found nil while unwrapping an Optional value . 登录导致fatal error: unexpectedly found nil while unwrapping an Optional value

在此处输入图片说明

Checked again on my iPhone. 在我的iPhone上再次检查。 Still working. 还在工作。

I'm using the latest FB SDK (did tried pod update): 我正在使用最新的FB SDK(尝试了pod更新):

pod 'FBSDKCoreKit' pod 'FBSDKLoginKit' pod 'FBSDKShareKit'

Xcode 8 & Swift 2.3 Xcode 8和Swift 2.3

I've tryed to put the REQUEST FB IMAGE block into an if statement to check for token != nil , but that resulted in crash within backendless.userService.loginWithFacebookSDK 我试图将REQUEST FB IMAGE块放入if语句中以检查token != nil ,但这导致backendless.userService.loginWithFacebookSDK崩溃

Why are there different results between physical iPhone (6S Plus iOS 10.0) and iPad Air2 (iOS 10.0)? 为什么物理iPhone(6S Plus iOS 10.0)和iPad Air2(iOS 10.0)之间会有不同的结果? Help is very appreciated. 非常感谢您的帮助。

PS: this line is in me didFinishLaunchingWithOptions : PS:这行在我中didFinishLaunchingWithOptions

FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

I will share my experience.This problem was in my app only in case when i call FBSDKAccessToken.currentAccessToken() before method : 我将分享我的经验。仅当我在方法之前调用FBSDKAccessToken.currentAccessToken()时,此问题才出现在我的应用程序中:

FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

Try to add this method to the AppDelegate file to the didFinishLaunchingWithOptions method. 尝试将此方法添加到didFinishLaunchingWithOptions方法的AppDelegate文件中。

Also take a look on FB official documentation 还可以看看FB官方文档

FBSDKLoginManager works directly with [FBSDKAccessToken currentAccessToken] and sets the "currentAccessToken" upon successful authorizations (or sets nil in case of logOut). FBSDKLoginManager直接与[FBSDKAccessToken currentAccessToken]一起使用,并在成功授权后设置“ currentAccessToken”(如果注销,则设置为nil)。 You should check [FBSDKAccessToken currentAccessToken] before calling logIn* to see if there is a cached token available (typically in your viewDidLoad). 您应在调用logIn *之前检查[FBSDKAccessToken currentAccessToken],以查看是否有可用的缓存令牌(通常在viewDidLoad中)。 If you are managing your own token instances outside of "currentAccessToken", you will need to set "currentAccessToken" before calling logIn* to authorize further permissions on your tokens. 如果要在“ currentAccessToken”之外管理自己的令牌实例,则需要在调用logIn *授予对令牌的进一步权限之前设置“ currentAccessToken”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM