ios13比较大的变化,苹果推出了自己的第三方登录。
Apple login免输入用户名密码,用很高的安全性,支持苹果所有的平台和web端
1,使用
加入支持库AuthenticationServices.framework
引用之
import AuthenticationServices
在工程的Signing & Capabilities中确认支持sign in with Apple
检查授权是否过期
let appleIDProvider = ASAuthorizationAppleIDProvider()
appleIDProvider.getCredentialState(forUserID:KeychainItem.currentUserIdentifier) { (credentialState, error)in
switchcredentialState {
case.authorized:
// The Apple ID credential is valid.
break
case.revoked:
// The Apple ID credential is revoked.
break
case.notFound:
// No credential was found, so show the sign-in UI.
default:
break
}
}
页面出现时检查授权和赋值delegate
let requests = [ASAuthorizationAppleIDProvider().createRequest(),
ASAuthorizationPasswordProvider().createRequest()]
// Create an authorization controller with the given requests.
letauthorizationController =ASAuthorizationController(authorizationRequests: requests)
authorizationController.delegate=self
authorizationController.presentationContextProvider=self
authorizationController.performRequests()
点击按钮触发事件
let appleIDProvider = ASAuthorizationAppleIDProvider()
letrequest = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
//用户昵称 邮箱
letauthorizationController =ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate=self
authorizationController.presentationContextProvider=self
authorizationController.performRequests()
实现delegate下的方法,获取AppleID数据
func authorizationController(controller:ASAuthorizationController, didCompleteWithAuthorization authorization:ASAuthorization) {
if let appleIDCredential = authorization.credentialas?ASAuthorizationAppleIDCredential{
letuserIdentifier = appleIDCredential.user
letfullName = appleIDCredential.fullName
letemail = appleIDCredential.email
//获取到的user、fullName、email
// Create an account in your system.
}elseifletpasswordCredential = authorization.credentialas?ASPasswordCredential{
// Sign in using an existing iCloud Keychain credential.
}
}
funcauthorizationController(controller:ASAuthorizationController, didCompleteWithError error:Error) {
// Handle error.
}
2,注意
如果App中使用第三方登录的,苹果要求必须实现Apple login,这是强制的!!
其他使用自身账号登录系统的不算在内。
遇到,了解,并记录!!