2015-09-15 72 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