1. 工程配置capability
- 首先需要在project中添加capability,找到sign in with apple
-
添加capability之后, 会发现entitlements文件会同步增加:
-
如果没找到entitlements文件, 可以去文件夹中找然后拖进工程里.
2. 苹果开发者网站添加 identifier + services IDs
- 然后进入苹果开发者网站, 进入证书页面
- 创建services id
- 注册id
3. 配置 services IDs
-
可以查看刚才配置的id, 在identifier分类可以查看.
-
然后继续打开刚创好的 service id:
- 把firebase回调配上去:
4. 配置keys
- firebase中还需要keys, 需要我们在苹果创建:
然后继续注册keys:
- 注意这个key要妥善保存好, 因为只能下载一次
5. 配置firebase auth - apple登录
在firebase控制台中, 把刚才苹果的配置的 services IDs, KEY写上去。
6. 写代码 - 使用 Apple 帐号登录并进行 Firebase 身份验证
如需进行 Apple 帐号身份验证,请先让用户使用 Apple 的 AuthenticationServices 框架登录其 Apple 帐号,然后使用 Apple 响应中的 ID 令牌来创建一个 Firebase AuthCredential 对象.
@available(iOS 13.0, *)
extension LoginVC: ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
self.view.window!
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
guard let nonce = currentNonce else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}
guard let appleIDToken = appleIDCredential.identityToken else {
print("Unable to fetch identity token")
return
}
guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("Unable to serialize token string from data: (appleIDToken.debugDescription)")
return
}
// Initialize a Firebase credential, including the user's full name.
let credential = OAuthProvider.appleCredential(withIDToken: idTokenString,
rawNonce: nonce,
fullName: appleIDCredential.fullName)
// Sign in with Firebase.
RKGUI.showLoading(in: view)
Auth.auth().signIn(with: credential) { result, error in
if error == nil {
self.thirdLogin()
} else {
RKGUI.hideAll()
self.handleFirebaseError(error)
}
}
}
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// Handle error.
print("Sign in with Apple errored: (error)")
}
}