如何使用Facebook SDK 4.6 for iOS登录?

Utk*_*maz 5 ios facebook-ios-sdk swift2

我正在使用此代码进行FB登录:

@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
                        }
                    })
                }
            } 
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

loginwithreadpermissions在SDK 4.6中已弃用

我该如何更改此代码?

Adi*_*mro 13

如果你查看文档,你会看到提到替代api.从文档中FBSDKLoginManager可以看出:

- (void)logInWithReadPermissions:(NSArray *)permissions
                         handler:(FBSDKLoginManagerRequestTokenHandler)handler
__attribute__((deprecated("use logInWithReadPermissions: fromViewController:handler: instead")));
Run Code Online (Sandbox Code Playgroud)

因此,有一种新方法,它还需要一个额外的参数UIViewController来确定登录序列的起始位置.正如文件所说:

- (void)logInWithReadPermissions:(NSArray *)permissions
              fromViewController:(UIViewController *)fromViewController
              handler:(FBSDKLoginManagerRequestTokenHandler)handler;
Run Code Online (Sandbox Code Playgroud)

并且参数的解释说:

fromViewController - 从中​​呈现的视图控制器.如果为nil,则将尽可能自动地确定最顶层的视图控制器.

因为,只有一个附加参数,您可以将其添加到现有实现中,如下所示:

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
})
Run Code Online (Sandbox Code Playgroud)

最新的xcode还应该在您编写时logInWithReadPermissions提供所有可用选项.