代码之家  ›  专栏  ›  技术社区  ›  Utku Dalmaz

如何使用iOS版Facebook SDK 4.6中的登录?

  •  5
  • Utku Dalmaz  · 技术社区  · 11 年前

    我使用此代码进行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
                            }
                        })
                    }
                } 
            }
        })
    }
    

    但是 loginwithreadpermissions SDK 4.6中已弃用

    我应该如何更改此代码?

    1 回复  |  直到 11 年前
        1
  •  13
  •   Adil Soomro    11 年前

    如果您查看文档,也会看到提到替代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 所有可用选项。

    推荐文章