KMAppleLoginManager.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // KMAppleLoginManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/8/11.
  6. //
  7. import Foundation
  8. import AuthenticationServices
  9. enum KMAppleLoginErrorType: String, CaseIterable {
  10. case unknown = "unknown"
  11. case failed = "Authorization failed" //"授权失败"
  12. case notHandled = "Failed to process authorization request" // "未能处理授权请求"
  13. case invalidResponse = "Invalid response to authorization request" // "授权请求响应无效"
  14. case requestFailed = "Authorization request failed" //"授权请求失败"
  15. case cancel = "Cancel authorization." //"取消授权"
  16. }
  17. typealias KMAppleLoginCompletion = (_ user: String, _ token: String, _ error: KMAppleLoginErrorType) -> Void
  18. class KMAppleLoginManager: NSObject {
  19. static let manager = KMAppleLoginManager()
  20. private var loginCompletion: KMAppleLoginCompletion?
  21. func login(_ completion: @escaping KMAppleLoginCompletion) {
  22. self.loginCompletion = completion
  23. let provider = ASAuthorizationAppleIDProvider.init()
  24. let request = provider.createRequest()
  25. let controller = ASAuthorizationController.init(authorizationRequests: [request])
  26. controller.delegate = self
  27. // controller.presentationContextProvider = self
  28. controller.performRequests()
  29. }
  30. }
  31. extension KMAppleLoginManager: ASAuthorizationControllerDelegate {
  32. func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
  33. var errorType: KMAppleLoginErrorType = .unknown
  34. switch error._code {
  35. case ASAuthorizationError.Code.canceled.rawValue:
  36. errorType = .cancel
  37. case ASAuthorizationError.Code.failed.rawValue:
  38. errorType = .requestFailed
  39. case ASAuthorizationError.Code.invalidResponse.rawValue:
  40. errorType = .invalidResponse
  41. case ASAuthorizationError.Code.notHandled.rawValue:
  42. errorType = .notHandled
  43. default:
  44. errorType = .failed
  45. }
  46. guard let callBack = self.loginCompletion else { return }
  47. callBack("", "", errorType)
  48. }
  49. func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
  50. if authorization.credential is ASAuthorizationAppleIDCredential { // 登录
  51. let credential = authorization.credential as! ASAuthorizationAppleIDCredential
  52. let user = credential.user
  53. let fullName = credential.fullName
  54. let email = credential.email
  55. let state = credential.state
  56. let identityToken = credential.identityToken
  57. let token = String.init(data: identityToken ?? Data(), encoding: .utf8)
  58. debugPrint("apple login success")
  59. guard let callBack = self.loginCompletion else { return }
  60. guard let resultToken = token else { return }
  61. callBack(user, resultToken, .unknown)
  62. } else if authorization.credential is ASPasswordCredential { // 使用现有的iCloud密钥链凭证登录。
  63. // let credential = authorization.credential as! ASPasswordCredential
  64. // let user = credential.user
  65. // let password = credential.password
  66. debugPrint("apple login failed")
  67. guard let callBack = self.loginCompletion else { return }
  68. callBack("", "", .failed)
  69. } else {
  70. debugPrint("apple login failed")
  71. guard let callBack = self.loginCompletion else { return }
  72. callBack("", "", .failed)
  73. }
  74. }
  75. }
  76. //extension KMAppleLoginManager: ASAuthorizationControllerPresentationContextProviding {
  77. // func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
  78. // return self.view.window!
  79. // }
  80. //}