1.简介
最新苹果系统13.0添加了苹果账号登录的模块,包括指纹和face id的登录;并且除去一些特殊的应用,项目中如果引入了三方登录(例如微信、QQ等),苹果账号登录是必选项。最近项目中需要进行三方登录的添加,就熟悉了一下苹果账号登录,记录一下。
2.苹果审核文档
3.集成步骤
3.1证书配置
证书配置完后,下载安装更新本地证书。
3.2项目配置
项目配置完后,添加苹果账号工具类,书写代码。
4.代码
苹果账号登录系统模块为:import AuthenticationServices
;我用的是单例工具类,登录触发按钮是全部自定义的,以下是代码。
4.1工具类代码
import Foundation
import AuthenticationServices
typealias ZBYAppleLoginToolSuccessClosures = (_ user: String, _ token: String) -> Void
typealias ZBYAppleLoginToolFailureClosures = (_ error: BaseError) -> Void
@available(iOS 13, *)
class ZBYAppleLoginTool: NSObject {
static let instence = ZBYAppleLoginTool.init()
private override init() {
}
override func copy() -> Any {
return ZBYAppleLoginTool.instence // self
}
override func mutableCopy() -> Any {
return ZBYAppleLoginTool.instence // self
}
// Optional
public func reset() {
// Reset all properties to default value
}
private weak var parentController: UIViewController?
private var successCompelecte: ZBYAppleLoginToolSuccessClosures?
private var failureCompelecte: ZBYAppleLoginToolFailureClosures?
public func isPast() -> Void {
let provider = ASAuthorizationAppleIDProvider.init()
provider.getCredentialState(forUserID: "") { (status, error) in
switch status {
case .revoked: do { // 已撤销
}
case .authorized: do { // 已授权
}
case .notFound: do { // 未发现
}
case .transferred: do { // 已转移
}
@unknown default:
break
}
}
}
public func show(success: ZBYAppleLoginToolSuccessClosures? = nil, failure: ZBYAppleLoginToolFailureClosures? = nil) {
self.successCompelecte = success
self.failureCompelecte = failure
let provider = ASAuthorizationAppleIDProvider.init()
let request = provider.createRequest()
let controller = ASAuthorizationController.init(authorizationRequests: [request])
controller.delegate = self
controller.presentationContextProvider = self
controller.performRequests()
}
}
@available(iOS 13, *)
extension ZBYAppleLoginTool: ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return kKeyWindow
}
}
@available(iOS 13, *)
extension ZBYAppleLoginTool: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
var baseError = BaseError.init()
baseError.code = Int.init(error._code)
switch baseError.code {
case ASAuthorizationError.Code.canceled.rawValue:
baseError.description = "取消授权"
case ASAuthorizationError.Code.failed.rawValue:
baseError.description = "授权请求失败"
case ASAuthorizationError.Code.invalidResponse.rawValue:
baseError.description = "授权请求响应无效"
case ASAuthorizationError.Code.notHandled.rawValue:
baseError.description = "未能处理授权请求"
default:
baseError.description = "授权失败"
}
self.failureCompelecte?(baseError)
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if authorization.credential is ASAuthorizationAppleIDCredential { // 登录
let credential = authorization.credential as! ASAuthorizationAppleIDCredential
let user = credential.user
// let fullName = credential.fullName
// let email = credential.email
guard let identityToken = credential.identityToken else {
self.failureCompelecte?(BaseError.init(0, "identityToken为空"))
return
}
guard let token = String.init(data: identityToken, encoding: .utf8) else {
self.failureCompelecte?(BaseError.init(0, "identityToken为空"))
return
}
self.successCompelecte?(user, token)
}
else if authorization.credential is ASPasswordCredential { // 使用现有的iCloud密钥链凭证登录。
// let credential = authorization.credential as! ASPasswordCredential
// let user = credential.user
// let password = credential.password
let baseError = BaseError.init(0, "授权失败")
self.failureCompelecte?(baseError)
}
else {
let baseError = BaseError.init(0, "授权失败")
self.failureCompelecte?(baseError)
}
}
}
4.2调用代码
if type == .apple {
if #available(iOS 13, *) {
ZBYAppleLoginTool.instence.show(success: { (user, token) in
NSLog(user)
}) { (error) in
MBProgressTool.show(to: self.view, text: error.description)
}
}
}
else {
}
转自:
作者:苍眸之宝宝
链接:https://www.jianshu.com/p/8371a477b1a4
来源:简书