在我的Xcode項目中,我已經有一個登錄功能,它使用用戶的用戶名和密碼創建。現在我想在該項目中集成Facebook登錄,但我不完全確定如何執行此操作。當有人通過他們的方式創建帳戶時,我現在擁有該帳戶,他們會創建並保存用戶名和密碼,然後只要他們想再次登錄,就可以將其與PFUser.logIn(withUsername: ..., password: ...)
一起使用。集成Facebook登錄與解析Swift 3
但與Facebook,我不知道什麼唯一的標識符我想保存他們的帳戶。我知道我可以提取一個Facebook ID,但是如何在獲取後登錄用戶?就像我之前說過的,我目前使用PFUser.logIn(withUsername: ..., password: ...)
來記錄用戶,然後我只用PFUser.current()?.username
來獲取與該用戶相關的所有數據。我聽說我想爲此使用PFFacebookUtils.logInInBackground
,但我已經嘗試過實現它,但是當我按下「繼續使用Facebook按鈕」時,應用程序崩潰(我截圖錯誤並將其放置在底部)。我已經複製了我迄今爲止所擁有的代碼。 如何將註冊與Facebook功能集成在一起,以便保存用戶的Facebook ID以及我用什麼方法對用戶進行簽名(即PFFacebookUtils.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年8月12日14:14:33.223472-0700項目[2423:10***終止應用程序由於未捕獲的異常'NSInternalInconsistencyException',原因是:
***第一擲調用堆棧 '你必須到+ initializeFacebookWithApplicationLaunchOptions調用初始化PFFacebookUtils': (0x188042fe0 0x186aa4538 0x188042f28 0x100b8f934 0x100b90020 0x100b9032c 0x10019eee8 0x1001a0a64 0x100867598 0X 10086e998 0x10086e4f0 0x100871a94 0x18b3610d4 0x101521a10 0x101526b78 0x187ff10c8 0x187feece4 0x187f1eda4 0x189989074 0x18e1dd364 0x1001ec288 0x186f2d59c) 的libC++ abi.dylib:與類型的未捕獲的異常終止NSException (LLDB)
您可以在應用程序崩潰後發佈日誌輸出嗎? 'PFFropicalUtils.logInInBackground'起作用,所以必須有另一個問題。 – nathan
好吧,我剛剛編輯錯誤的帖子。 – fphelp
我的意思是控制檯輸出(作爲文本,而不是圖像)。這只是Xcode中的一個異常斷點。 – nathan