我已将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件事:
谢谢:)
答案 0 :(得分:0)
这是因为您在AppDelegate中的FBSDKApplicationDelegate.sharedInstance()。application(application,didFinishLaunchingWithOptions:launchOptions)之前调用FBSDKAccessToken.currentAccessToken()。