我正在提示我的用户通过 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()
}
}
}
}
在我将应用程序从开发模式转变为公共(public)模式之前,它运行良好。现在,每次我单击“在 Facebook 上登录”按钮时,它都会将我带到带有 Facebook 页面的 Safari,并显示“错误。未登录:您尚未登录。请登录并重试”强>
有人知道这个问题可能是什么吗?登录页面不再提示。
最佳答案
我发现您正在尝试使用 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并选择您的应用程序。在左侧面板上,选择“设置”。检查所有应用程序信息,尤其是“ bundle ID”(如果正确,请将其替换为另一个应用程序,保存设置,然后放回正确的设置)。如果问题仍然存在,请转到您的 Facebook 应用程序主页并阅读“Facebook SDK 入门”指南(并在出现提示时检查包标识符是否正确)。另外,在“客户端 OAuth 设置”(顶部的“高级”选项卡)中,我唯一启用的是客户端 OAuth 登录
关于ios - Facebook 登录 Swift 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883201/