2015-09-15 71 views
5

我使用此代碼FB登錄:如何在iOS SDK的Facebook SDK 4.6中使用登錄?

@IBAction func fbloginbtn(sender: AnyObject) { 

    FBSDKLoginManager().logInWithReadPermissions(["public_profile", "email","user_location","user_about_me", "user_photos", "user_website"], handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in 
     if (error == nil){ 
      let fbloginresult : FBSDKLoginManagerLoginResult = result 
      if(fbloginresult.grantedPermissions.contains("email")) 
      { 
       if((FBSDKAccessToken.currentAccessToken()) != nil){ 
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in 
         if (error == nil){ 
          //do sth 
         } 
        }) 
       } 
      } 
     } 
    }) 
} 

loginwithreadpermissions在SDK 4.6

不贊成我應該如何改變這種代碼?

+0

爲了確保4.6是** Facebook ** SDK版本,對不對?不是** iOS ** SDK版本? (那會太舊了)。 –

+1

oh對不起,這是適用於iOS的Facebook SDK –

+0

也許對於熟悉Facebook SDK的人來說顯而易見,但對於其他人來說,它可能會有些混亂。感謝您修復它。 –

回答

13

如果您查看文檔,您也會看到提及替代API。 從FBSDKLoginManager的文檔,它說:

- (void)logInWithReadPermissions:(NSArray *)permissions 
         handler:(FBSDKLoginManagerRequestTokenHandler)handler 
__attribute__((deprecated("use logInWithReadPermissions: fromViewController:handler: instead"))); 

因此,有一種新的方法,即利用了UIViewController附加參數,以及從登錄序列從什麼地方來確定。作爲文檔說:

- (void)logInWithReadPermissions:(NSArray *)permissions 
       fromViewController:(UIViewController *)fromViewController 
       handler:(FBSDKLoginManagerRequestTokenHandler)handler; 

和參數的解釋說:

fromViewController - 視圖控制器以從呈現。如果爲零,則最高視圖控制器將自動確定爲最佳 。

因爲,只有一個額外的參數,你可以把它添加到現有的實現是這樣的:

FBSDKLoginManager().logInWithReadPermissions(["public_profile", "others"], 
          fromViewController:self //<=== new addition, self is the view controller where you're calling this method. 
          handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in 
}) 

最新的Xcode還應該建議你,當你寫,logInWithReadPermissions所有可用的選項。

+1

在swift中的一個例子對我來說是理想的初學者。 –

+0

看我的編輯,我已經添加了一個簡單的代碼swift。 –

+0

@AdilSoomro我用來檢查我的令牌是否有效,以確定用戶是否需要再次登錄。你有什麼?這裏曾經是我的代碼:「if([FBSession activeSession] .state == FBSessionStateOpen)」。 – Felipe