swift 如何在使用 Apple 登录时获取凭据
How to get credentials on signIn with Apple in swift
我知道这个问题不是第一次问,但我卡住了...
我尝试用苹果登录,但在获取用户数据时遇到问题,我的代码如下
import UIKit
import AuthenticationServices
class SignInWithAppleVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupProviderLoginView()
}
func setupProviderLoginView() {
let appleButton = ASAuthorizationAppleIDButton()
appleButton.frame = CGRect(x: 20, y: (Int(UIScreen.main.bounds.size.height) - 150), width: (Int(UIScreen.main.bounds.size.width) - 40), height: 50)
appleButton.addTarget(self, action: #selector(signInWithApple), for: .touchUpInside)
self.view.addSubview(appleButton)
}
@objc func signInWithApple() {
print("sign in with apple clicked")
let appleIdProvider = ASAuthorizationAppleIDProvider()
let request = appleIdProvider.createRequest()
request.requestedScopes = [.email,.fullName]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
}
extension SignInWithAppleVC : ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
let alert = UIAlertController(title: "ERROR", message: "\(error.localizedDescription)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
case let credentials as ASAuthorizationAppleIDCredential:
print(credentials.user) // Here we get results only first time
print(credentials.fullName?.givenName!)//
print(credentials.fullName?.familyName)//
//MARK: - Use credentials
case let credintials as ASPasswordCredential :
print(credintials.password)
default:
let alert = UIAlertController(title: "Apple SgnIn", message: "Something went wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}
}
extension SignInWithAppleVC : ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return view.window!
}
}
问题是,我只在第一次尝试时收到电子邮件、名字、姓氏。但每次下一次我都没有得到回应。我是否需要访问用户日期的钥匙串(如果是,那么如何)或任何其他解决方案?任何帮助,link,示例代码将不胜感激。提前致谢
根据 Apple 文档,您只会在第一次尝试时获得用户标识符、姓名和电子邮件地址信息,您需要将这些信息存储在服务器或本地数据库中以便下次访问。
新时代,当您使用相同的帐户登录时,您将只获得用户标识符,您需要从数据库中获取其他信息。
参考 Apple 文档:Link
我知道这个问题不是第一次问,但我卡住了... 我尝试用苹果登录,但在获取用户数据时遇到问题,我的代码如下
import UIKit
import AuthenticationServices
class SignInWithAppleVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupProviderLoginView()
}
func setupProviderLoginView() {
let appleButton = ASAuthorizationAppleIDButton()
appleButton.frame = CGRect(x: 20, y: (Int(UIScreen.main.bounds.size.height) - 150), width: (Int(UIScreen.main.bounds.size.width) - 40), height: 50)
appleButton.addTarget(self, action: #selector(signInWithApple), for: .touchUpInside)
self.view.addSubview(appleButton)
}
@objc func signInWithApple() {
print("sign in with apple clicked")
let appleIdProvider = ASAuthorizationAppleIDProvider()
let request = appleIdProvider.createRequest()
request.requestedScopes = [.email,.fullName]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
}
extension SignInWithAppleVC : ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
let alert = UIAlertController(title: "ERROR", message: "\(error.localizedDescription)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
case let credentials as ASAuthorizationAppleIDCredential:
print(credentials.user) // Here we get results only first time
print(credentials.fullName?.givenName!)//
print(credentials.fullName?.familyName)//
//MARK: - Use credentials
case let credintials as ASPasswordCredential :
print(credintials.password)
default:
let alert = UIAlertController(title: "Apple SgnIn", message: "Something went wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}
}
extension SignInWithAppleVC : ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return view.window!
}
}
问题是,我只在第一次尝试时收到电子邮件、名字、姓氏。但每次下一次我都没有得到回应。我是否需要访问用户日期的钥匙串(如果是,那么如何)或任何其他解决方案?任何帮助,link,示例代码将不胜感激。提前致谢
根据 Apple 文档,您只会在第一次尝试时获得用户标识符、姓名和电子邮件地址信息,您需要将这些信息存储在服务器或本地数据库中以便下次访问。
新时代,当您使用相同的帐户登录时,您将只获得用户标识符,您需要从数据库中获取其他信息。
参考 Apple 文档:Link