即使应用程序被杀,也可以保持Facebook访问权限

时间:2015-10-07 04:18:47

标签: swift2 facebook-ios-sdk

我已将Facebook SDK应用到我的Swift应用程序中,并使用登录按钮将用户重定向到Facebook:

@IBAction func btnFBLoginPressed(sender: AnyObject) {
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

    fbLoginManager .logInWithReadPermissions(["public_profile", "email", "user_friends"], fromViewController:self, handler: { (result, error) -> Void in
        if ((error) != nil)
        {
            print("Error: \(error)")
        }
        else if result.isCancelled {
            let facebookCancelled = UIAlertController(title: "Facebook Cancelled", message: "You cancelled Facebook sign up.", preferredStyle: UIAlertControllerStyle.Alert)
            self.presentViewController(facebookCancelled, animated: true, completion: nil)

            facebookCancelled.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: { action in
            }))
        }
        else
        {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if result.grantedPermissions.contains("email")
            {
                // Do work
            }

            self.getFBUserData()
        }
    })
}

func getFBUserData(){
    if((FBSDKAccessToken.currentAccessToken()) != nil){
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(normal), email"])
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
            if ((error) != nil)
            {
                // Process error
                print("Error: \(error)")
            }
            else
            {
                print("fetched user: \(result)")
                self.facebookTestLabel.text = "\(result)"

                //let fullName : NSString = result.valueForKey("name") as! NSString
                //print("Full name is: \(fullName)")
            }
        })
    }
}

2件事:

  1. 即使我的应用被杀死并重新加载,我如何维护Facebook访问令牌?
  2. 如果安装Facebook应用程序而不是重定向到网站,我如何使用它?
  3. 谢谢:)

1 个答案:

答案 0 :(得分:0)

这是因为您在AppDelegate中的FBSDKApplicationDelegate.sharedInstance()。application(application,didFinishLaunchingWithOptions:launchOptions)之前调用FBSDKAccessToken.currentAccessToken()。