2015-10-07 81 views
0

我已經在實施的Facebook SDK到我的雨燕的應用程序,並有一個登錄按鈕使用將用戶重定向至Facebook:保持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)") 
      } 
     }) 
    } 
} 

兩件事:

  1. 即使我的應用程序被殺死並重新加載,我如何維護Facebook訪問令牌?
  2. 如何安裝Facebook應用程序而不是重定向到網站?

謝謝:)

+0

我沒有在iOS上使用Facebook SDK,但我在Android上使用。當我的手機有Facebook應用程序並且它已經登錄時,我的應用程序在打開時仍然可以訪問FB,儘管我殺了它。 –

+0

謝謝。我需要做的不是訪問FB,但如果我重新加載我的應用程序,則訪問來自已經過身份驗證的用戶的public_profile,圖像和電子郵件。如果用戶註銷我的應用程序,我理解我需要重新授權,但用戶可能只需重新啓動手機。 – puks1978

+0

我不確定這是否是好方法,但您可以將Facebook訪問令牌保存在鑰匙串中。所以即使你刪除了你的應用程序,你可以訪問時再次安裝它。 – Sujit

回答

0

這是發生,因爲你在呼喚FBSDKApplicationDelegate.sharedInstance前FBSDKAccessToken.currentAccessToken()()應用程序(應用程序,didFinishLaunchingWithOptions:launchOptions)在Ap​​pDelegate中。

相關問題