iOS 苹果登录 swift版

苹果登录
项目中继承第三方登录时,需增加上苹果登录即可上架
苹果登录需要iOS系统 13以上支持
详细的内容阅读苹果官方的网址
url:https://developer.apple.com/documentation/authenticationservices/implementing_user_authentication_with_sign_in_with_apple
- 苹果登录的前期工作:
- 1.开发者账号中增加苹果登录的选项
WeChat23b1385ee0d348d98848101825401b1c.png
1.1  可能会造成证书无法使用,重新编辑一下保存下载即可!
WeChat268e1a505c3429a5cca91eec93d9dfce.png
- 2.xcode中配置苹果登录
WechatIMG15745.jpeg
前期的配置基本上完成
剩下的就是代码逻辑
- 3.代码中增加苹果登录的逻辑
//swift版本的代码逻辑
//头文件
import AuthenticationServices

//按钮加载   苹果登录 对于按钮有一定的要求,具体查看上方的连接
// 此处使用了一个临时的
if #available(iOS 13.0, *) {
            let authorizationButton = ASAuthorizationAppleIDButton()
            authorizationButton.frame = CGRect(x: (KScreenWidth - 300) / 2, y: kScreenHeight - 50, width: 300, height: 30)
            authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
            self.view.addSubview(authorizationButton)
        } else {
            // Fallback on earlier versions
        }


 //MARK: 点击苹果登陆按钮
    @objc
    func handleAuthorizationAppleIDButtonPress() {
        
        if #available(iOS 13.0, *) {
            /**
             - 点击 苹果登录的按钮跳出苹果登录的界面
             - 跳转出系统界面
             */
            let appleIDProvider = ASAuthorizationAppleIDProvider()
            let request = appleIDProvider.createRequest()
            request.requestedScopes = [.fullName, .email]
            
            let authorizationController = ASAuthorizationController(authorizationRequests: [request])
            authorizationController.delegate = self
            authorizationController.presentationContextProvider = self as? ASAuthorizationControllerPresentationContextProviding
            authorizationController.performRequests()

        } else {
            // Fallback on earlier versions
        }
        
    }

//MARK: - 授权成功
    @available(iOS 13.0, *)
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if #available(iOS 13.0, *) {
            switch authorization.credential {
            case let appleIDCredential as ASAuthorizationAppleIDCredential:
                /**
              - 首次注册 能够那去到的参数分别是:
              1. user
              2.state
              3.authorizedScopes
              4.authorizationCode
              5.identityToken
              6.email
              7.fullName
              8.realUserStatus
                */
                // Create an account in your system.
                let userIdentifier = appleIDCredential.user
                let fullName = appleIDCredential.fullName
                let email = appleIDCredential.email
                let code = appleIDCredential.authorizationCode
                // For the purpose of this demo app, store the `userIdentifier` in the keychain.
                self.saveUserInKeychain(userIdentifier)
                
                // For the purpose of this demo app, show the Apple ID credential information in the `ResultViewController`.
                self.showResultViewController(userIdentifier: userIdentifier, fullName: fullName, email: email)
                BPLog.lmhInfo("userID:\(userIdentifier),fullName:\(fullName),userEmail:\(email),code:\(code)")
            case let passwordCredential as ASPasswordCredential:
                
                // Sign in using an existing iCloud Keychain credential.
                let username = passwordCredential.user
                let password = passwordCredential.password
                
                // For the purpose of this demo app, show the password credential as an alert.
                DispatchQueue.main.async {
                    self.showPasswordCredentialAlert(username: username, password: password)
                }
                
            default:
                break
            }
        } else {
            // Fallback on earlier versions
        }
    }

你可能感兴趣的:(iOS 苹果登录 swift版)