与Apple登录:如何为现有应用实现它? [英] Sign In with Apple: How to achieve it for existing app?

查看:62
本文介绍了与Apple登录:如何为现有应用实现它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着Apple在2019年6月3日发布的最新重大更新,有一项功能

With recent major updates released by Apple on 3rd June 2019, there is one feature Sign In with Apple. Information about 'how to use this' in app is available but I can see any sample source code, how to achieve this feature in your existing iOS app.

我正在寻找示例源代码,因为我不知道如何开始.

I'm looking for a sample source code, as I can't understand how to start with this.

我尝试过的操作:使用Apple登录

推荐答案

第一步是您需要 import AuthenticationServices

        let appleIDProvider = ASAuthorizationAppleIDProvider()
        appleIDProvider.getCredentialState(forUserID: KeychainItem.currentUserIdentifier) { (credentialState, error) in
            switch credentialState {
            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
            }
        }

如何使用Apple创建登录名

第1步

如果找到现有的iCloud钥匙串凭证或Apple ID凭证,则提示用户.将 ASAuthorizationControllerDelegate 实现为

Prompts the user if an existing iCloud Keychain credential or Apple ID credential is found. implement ASAuthorizationControllerDelegate to

func performExistingAccountSetupFlows() {
    // Prepare requests for both Apple ID and password providers.
    let requests = [ASAuthorizationAppleIDProvider().createRequest(),
                    ASAuthorizationPasswordProvider().createRequest()]
    
    // Create an authorization controller with the given requests.
    let authorizationController = ASAuthorizationController(authorizationRequests: requests)
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

extension ViewController: ASAuthorizationControllerDelegate {
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
         //here is credentials . 
        }
    }
}

extension ViewController: ASAuthorizationControllerPresentationContextProviding {
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        return self.view.window!
    }
}

第二步:

创建按钮

    let authorizationButton = ASAuthorizationAppleIDButton()
    authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)

Step3

@objc
func handleAuthorizationAppleIDButtonPress() {
    let appleIDProvider = ASAuthorizationAppleIDProvider()
    let request = appleIDProvider.createRequest()
    request.requestedScopes = [.fullName, .email]
    
    let authorizationController = ASAuthorizationController(authorizationRequests: [request])
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

可用性: iOS 13或更高版本

演示应用程序:Github上提供了具有钥匙串集成功能的完整的演示应用程序- https://github.com/developerinsider/Sign-In-with-Apple-Demo

Demo Application: A complete working demo application with keychain integration available on Github - https://github.com/developerinsider/Sign-In-with-Apple-Demo

注意:我很快会用越来越多的有用信息来更新答案.

Note: I will update answer with more and more useful information soon.

希望这会有所帮助.

这篇关于与Apple登录:如何为现有应用实现它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆