杀死App(iOS)

时间:2018-03-20 13:32:15

标签: ios swift facebook-login facebook-access-token fbsdk

我正在使用 FacebookSDK 开发一个iOS应用程序来登录。

登录并终止应用后,currentAccessToken始终为零。只有当我按下主页按钮并重新打开应用程序仍然在后台运行时,它才有效。

我开始使用警报,因为在杀死应用程序后日志无法正常工作。

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

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

    let accessToken = FBSDKAccessToken.current()
    if accessToken == nil {
        Auth.auth().addStateDidChangeListener { (auth, user) in
            alert(title: user?.uid ?? "1")
        }
        alert(title: "2")
    } else {
        alert(title: accessToken?.userID ?? "3")
    }
}

警告中始终显示“1”。

这是一个类似的问题: FBSDKAccessToken currentAccessToken nil after quitting app 要么 iOS 9 Facebook Access Token is nil on future app launches

我已经尝试过了:

  1. 钥匙串不起作用
  2. 通过Cocoapods降级至4.17
  3. 更改传输安全性不正常
  4. 帮助/建议,

    提前致谢。

1 个答案:

答案 0 :(得分:1)

AccessToken.current不会持续存在,因此您必须手动保存访问令牌字符串并在连续启动时重新创建它。

基本上:

  1. 获取访问令牌时保存

    let strAuthenticationToken = accessToken.authenticationToken
    UserDefaults.standard.set(strAuthenticationToken,
                          forKey: "AccessToken_Facebook")
    
  2. 获取访问令牌字符串并在下次启动时重新创建AccessToken对象

    if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
    //Create AccessToken for facebook login
    let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
    
    //...Skip Login and directly proceed to get facebook profile data with the AccessToken
    }
    
  3. 注意:为了便于此示例,Facebook Access令牌字符串保存到UserDefaults,但理想情况下应将其保存到钥匙串。

    在示例中:

    func loginWithFacebook() {
        //Check for previous Access Token
        if let accessToken  = AccessToken.current {
            //AccessToken was obtained during same session
            getAccountDetails(withAccessToken: accessToken)
        }
        else if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
            //A previous access token string was saved so create the required AccessToken object
            let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
    
            //Skip Login and directly proceed to get facebook profile data with an AccessToken
            getAccountDetails(withAccessToken: accessToken)
        }
        else {
            //Access Token was not available so do the normal login flow to obtain the Access Token
            doFacebookLogin()
        }
    }
    
    func doFacebookLogin() {
        LoginManager().logIn(readPermissions: [.publicProfile, .email], viewController: nil) { loginResult in
            switch loginResult {
            case .failed(let error):
                print(error)
    
            case .cancelled:
                print("User cancelled login.")
    
            case .success(let grantedPermissions,
                          let declinedPermissions,
                          let accessToken):
    
                //Save Access Token string for silent login purpose later
                let strAuthenticationToken = accessToken.authenticationToken
                UserDefaults.standard.set(strAuthenticationToken,
                                          forKey: "AccessToken_Facebook")
    
                //Proceed to get facebook profile data
                self.getAccountDetails(withAccessToken: accessToken)
            }
        }
    }
    
    func getAccountDetails(withAccessToken accessToken: AccessToken) {
        let graphRequest: GraphRequest = GraphRequest(graphPath     : "me",
                                                      parameters    : ["fields" : "id, name, email"],
                                                      accessToken   : accessToken,
                                                      httpMethod    : GraphRequestHTTPMethod.GET,
                                                      apiVersion    : GraphAPIVersion.defaultVersion)
        graphRequest.start { (response, result) in
            switch result {
            case .success(let resultResponse):
                print(resultResponse)
            case .failed(let error):
                print(error)
            }
        }
    }
    

    (Swift 4 / Facebook SDK 4.30)