Facebook登录Swift项目

时间:2015-08-07 17:11:58

标签: ios facebook swift

我提示我的用户通过Facebook登录以获取应用程序个人资料的个人资料照片。这是我申请Facebook登录的代码:

func facebookSignIn() {
    //Current user that's logged in from sign up view
    let currUser = PFUser.currentUser()

    //Create a Facebook session
    FBSession.openActiveSessionWithReadPermissions(permission, allowLoginUI: true, completionHandler: {
        (session, status, error) -> Void in
            FBRequestConnection.startForMeWithCompletionHandler {(connection, result, error) -> Void in
            if error == nil {
                if result != nil && result.objectID != nil {
                    //Create a column in Parse called "facebookID"
                    currUser!.setObject(result.objectID as String, forKey: "facebookID")
                    currUser!.saveInBackground()
                }
            }
    }
}

在我将应用程序从开发模式转为公共之前,它运行良好。现在,每次我点击我的"登录Facebook"按钮,它带我到Facebook的Facebook页面,并说"错误。未登录:您尚未登录。请先登录并再试一次"

任何人都知道这个问题会是什么?登录页面不再提示。

2 个答案:

答案 0 :(得分:3)

我发现您正在尝试使用Facebook和Parse iOS SDK实现Facebook登录。我不确定您是否真的有必要使用FBSession和FBRequestConnection进行登录。实际上有两种方法可以去。第一个是简单地使用Parse Facebook Utils来处理登录。或者您可以通过Facebook iOS SDK实现登录。您可以在下面找到第二个选项的代码以及用户注册。

override func viewDidLoad(){
    FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "fb:", name: FBSDKProfileDidChangeNotification, object: nil) // hi! we have logged in! so we can get profile picture in "fb" function (since FBSDKProfile.currentProfile() which we loaded asynchronously is now fully downloaded)
}

@IBAction func loginWithFacebook(sender: AnyObject) { // button for login
    let fbLoginManager = FBSDKLoginManager()
    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
    fbLoginManager.logInWithReadPermissions(["email", "public_profile", "user_friends"], handler: {
        (result: FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
        if error == nil && result.token != nil {
            // logged in
        } else {
            // process error
        }
    })
}

func fb(notif:NSNotification){ // access token has changed! We have logged in or logged out
    if FBSDKAccessToken.currentAccessToken() != nil { // Are we logged in?
        PFFacebookUtils.logInInBackgroundWithAccessToken(FBSDKAccessToken.currentAccessToken(), block: { // Let`s login the fb user with parse (using acquired fb access token)
            (user: PFUser?, error: NSError?) -> Void in
            if let user = user {
                if user.isNew { // user is new? Then let`s register in Parse
                    let smallProfileImage = FBSDKProfile.currentProfile().imagePathForPictureMode(FBSDKProfilePictureMode.Normal, size: CGSizeMake(100, 100))  // // get user profile picture in a small size
                    let bigProfileImage = FBSDKProfile.currentProfile().imagePathForPictureMode(FBSDKProfilePictureMode.Normal, size: CGSizeMake(600, 600)) // get user profile picture in a big size
                    user.setObject("https://graph.facebook.com/\(smallProfileImage)", forKey: "smallProfileImage") // set user`s small image (link)
                    user.setObject("https://graph.facebook.com/\(bigProfileImage)", forKey: "bigProfileImage") // set user`s small image (link)
                    PFFacebookUtils.linkUserInBackground(user, withAccessToken: FBSDKAccessToken.currentAccessToken()) // save the user in Parse
                }
            } else {
                print("Uh oh. There was an error logging in.")
            }
        })

    }

}

更新:似乎我已经弄明白了。转到Facebook dev portal page并选择您的应用。在左侧面板上,选择"设置"。检查所有应用信息,特别是"捆绑ID" (如果正确,请将其替换为另一个,保存设置,然后重新输入正确的设置)。如果问题仍然存在,请访问您的Facebook应用程序主页,然后浏览“开始使用Facebook SDK"指导(并在提示时检查包标识符是否正确)。此外,在"客户端OAuth设置" (顶部的高级选项卡)我唯一启用的是Client OAuth Login

答案 1 :(得分:1)

也许您应该阅读有关FBSDKLoginButton here的内容,因为当您想要在应用中使用Facebook登录时,Facebook会告诉您使用openActiveSessionWithReadPermissions