在我的 Xcode 项目中,我已经有一个使用用户创建的用户名和密码的登录功能。现在我想在项目中集成一个 Facebook 登录,但我不完全确定该怎么做。当有人通过我现在拥有的方式创建帐户时,他们会创建并保存用户名和密码,然后可以将其用于 PFUser.logIn(withUsername: ..., password: ...)
每当他们想再次登录时。
但是对于 Facebook,我不知道应该使用什么唯一标识符来保存他们的帐户。我知道有一个我可以提取的 Facebook id,但是在我得到这个之后如何登录用户?就像我之前说的,我目前使用 PFUser.logIn(withUsername: ..., password: ...)
来登录用户,然后我只使用 PFUser.current()?。 username
获取与该用户相关的所有数据。我听说我应该为此使用 PFFFacebookUtils.logInInBackground
,但我已经尝试实现它但是当我按下“继续使用 Facebook 按钮”时,应用程序崩溃(我截图错误并将其放置在底部)。我已经复制了我目前拥有的代码。 如何将注册与 Facebook 功能集成,以便保存用户的 Facebook id,以及我使用什么方法让用户登录(即 PFFFacebookUtils.logInInBackground
)? 到目前为止,这是我的代码:
@IBOutlet var facebookSignUpButton: FBSDKLoginButton!
var fullnameFB = String()
var idFB = String()
var emailFB = String()
var isFBSignUp = Bool()
override func viewDidLoad() {
super.viewDidLoad()
signUpWithFacebook()
}
func signUpWithFacebook() {
facebookSignUpButton.readPermissions = ["email", "public_profile"]
facebookSignUpButton.delegate = self
self.view.addSubview(facebookSignUpButton)
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if error != nil { //if theres an error
print(error)
} else if result.isCancelled { // if user cancels the sign up request
print("user cancelled login")
} else {
PFFacebookUtils.logInInBackground(with: result!.token!) { (user, error) in
if error == nil {
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
} else {
print("User logged in through Facebook!")
}
if result.grantedPermissions.contains("email") {
if let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, name"]) {
graphRequest.start(completionHandler: { (connection, result, error) in
if error != nil {
print(error?.localizedDescription ?? String())
} else {
if let userDetails = result as? [String: String]{
print(userDetails)
self.fullnameFB = userDetails["name"]!
self.idFB = userDetails["id"]!
self.emailFB = userDetails["email"]!
self.isFBSignUp = true
}
}
})
}
} else {
print("didnt get email")
self.createAlert(title: "Facebook Sign Up", message: "To signup with Facebook, we need your email address")
}
} else {
print("Error while trying to login using Facebook: \(error?.localizedDescription ?? "---")")
}
} else {
print(error?.localizedDescription ?? String())
}
}
}
}
控制台输出:
2017-08-12 14:14:33.223472-0700 Project[2423:1001235] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'You must initialize PFFacebookUtils with a call to +initializeFacebookWithApplicationLaunchOptions'
*** First throw call stack: (0x188042fe0 0x186aa4538 0x188042f28 0x100b8f934 0x100b90020 0x100b9032c 0x10019eee8 0x1001a0a64 0x100867598 0x10086e998 0x10086e4f0 0x100871a94 0x18b3610d4 0x101521a10 0x101526b78 0x187ff10c8 0x187feece4 0x187f1eda4 0x189989074 0x18e1dd364 0x1001ec288 0x186f2d59c) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
最佳答案
如文档中所述,您需要在使用前初始化 PFFFacebookUtils
,如下所示:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// CODE...
PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)
}
重要提示:必须在 Parse 设置之后完成(Parse.initialize...
)。
关于swift - 将 Facebook 登录与 Parse Swift 3 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45654927/