一、需求场景

越来越多的App为了便利用户的方便登录,现已采用QQ微信支付宝微博等一些系列方便登录的方法。但是在苹果最新方针傍边,苹果表示:咋回事儿?瞧不起我么?!

SO,苹果要求运用了第三方登录的平台,必需要提供苹果登录通道,并且还必须在款式上不得被忽视。必须保证大小和款式不差异于其他第三方登录的方法。别的说一句,假如你不想做,想审阅的时分藏起来?

那么可能会触发2.3.1审阅条款,同时可能下次审阅时间将会被延时审阅,因小失大

二、Xocde装备

1.增加 sign In With Apple

苹果授权登录开发指南—swift版

2.装备成功后Xcode会自动增加环境变量SignInWithApple如图:

苹果授权登录开发指南—swift版

3.其他

Xcode 请登录着开发者账号进行装备,本文是登录这开发者账号的。

三、客户端代码实现-Swift

1.运用苹果官方提供的款式,参考链接

官方按钮款式截图:

苹果授权登录开发指南—swift版

2.自定义登录按钮

import AuthenticationServices
class LoginView: {
    func createView() {
        if #available(iOS 13.0, *) {
            NotificationCenter.default.addObserver(self, selector: #selector(handleSignInWithAppleStateChanged(noti:)), name: ASAuthorizationAppleIDProvider.credentialRevokedNotification, object: nil)
            let btn_apple = UIButton(type: .custom)
            btn_apple.addTarget(self, action: #selector(appleLogininAction), for: .touchUpInside)
            btn_apple.setBackgroundImage(UIImage(named: "youImage"), for: .normal)
            self.addSubview(btn_apple)
            btn_apple.snp.makeConstraints {
                $0.width.equalTo(33)
            }
        }
    }
    @objc func appleLogininAction() {
        if #available(iOS 13.0, *) {
            //不要运用let requests = [ASAuthorizationAppleIDProvider().createRequest(), ASAuthorizationPasswordProvider().createRequest()]
//ASAuthorizationPasswordProvider().createRequest()在第一次用苹果登录授权的时分会报错ASAuthorizationErrorUnknown 1000
            let requests = [ASAuthorizationAppleIDProvider().createRequest()]
            let authorizationController = ASAuthorizationController(authorizationRequests: requests)
            authorizationController.delegate = self
            authorizationController.presentationContextProvider = self
            authorizationController.performRequests()
        } else {
            // 处理不支持体系版别
        }
    }
    deinit {
        if #available(iOS 13.0, *) {
            NotificationCenter.default.removeObserver(self, name: ASAuthorizationAppleIDProvider.credentialRevokedNotification, object: nil)
        }
    }
}
@available(iOS 13.0, *)
extension LoginView: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
    func authorizationController(controller:ASAuthorizationController, didCompleteWithAuthorization authorization:ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential{
            // 苹果用户仅有标识符,该值在同一个开发者账号下的一切 App 下是一样的,开发者能够用该仅有标识符与自己后台体系的账号体系绑定起来。
            let user = appleIDCredential.user
            // 苹果用户信息 假如授权过,可能无法再次获取该信息
            let fullName = appleIDCredential.fullName
            let email = appleIDCredential.email
            // 服务器验证需要运用的参数
            let authorizationCode = String(data: appleIDCredential.authorizationCode!, encoding: String.Encoding.utf8)!
            let identityToken = String(data: appleIDCredential.identityToken!, encoding: String.Encoding.utf8)!
            // 用于判别当时登录的苹果账号是否是一个实在用户,取值有:unsupported、unknown、likelyReal
            let realUserStatus = appleIDCredential.realUserStatus;
            //对接登录接口,处理用户登录操作
        }
    }
    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        if let e = error as? ASAuthorizationError {
            var errorMsg = ""
            switch e.code {
            case .unknown:
                errorMsg = "授权恳求失利未知原因"
            case .canceled:
                errorMsg = "用户取消了授权恳求"
            case .invalidResponse:
                errorMsg = "授权恳求响应无效"
            case .notHandled:
                errorMsg = "未能处理授权恳求"
            case .failed:
                errorMsg = "授权恳求失利"
            @unknown default:
                errorMsg = "授权恳求失利其他原因"
            }
        }
    }
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return UIApplication.shared.keyWindow!
    }
}

四、避坑指南

1.装备后一向报错ASAuthorizationError.unknown

//走失利函数
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
//code":1000,"domain":"com.apple.AuthenticationServices.AuthorizationError
}
  • 解决方案

不要运用ASAuthorizationPasswordProvider().createRequest()!!!! ASAuthorizationPasswordProvider().createRequest()在第一次用苹果登录授权的时分会报错ASAuthorizationErrorUnknown 1000。但是假如你现已授权成功登录app了,第2次登录app授权时运用ASAuthorizationPasswordProvider创立request则不会报错unknow

2.提审注意事项

1.不要运用手机绑定!不要运用手机绑定!不要运用手机绑定!

苹果认为他们官方的这种登录方法现已满足安全,不需要在认证用户手机号的方法进行绑定信息。这块事务逻辑开发者能够自行调整。

2.需要刊出功能,否则会触发 5.1.1条款。

被拒原文:

Guideline 5.1.1(v) - Data Collection and Storage
We noticed that your app supports account creation but does not appear to include an option to initiate account deletion.
Apps that support account creation must also offer account deletion to give App Store users more control of the data they've shared while using your app.
Next Steps
If your app already supports account deletion, reply to this message and let us know how to locate this feature. If your app does not support account deletion, revise your app to include an option to initiate account deletion and delete all user data you are not legally required to retain.
If you are unable to offer account deletion or need to provide additional customer service flows to facilitate and confirm account deletion, either because your app operates in a highly-regulated industry or for some other reason, reply to this message in App Store Connect and provide additional information or documentation. If you have questions regarding your legal obligations, check with your legal counsel.

五、登录按钮资料

苹果授权登录开发指南—swift版

苹果授权登录开发指南—swift版