iOS项目框架搭建Swift编程

iOS 13 苹果授权登陆(Sign in with Apple

2019-10-18  本文已影响0人  NicoalsNC

快速集成苹果授权登陆(Sign in with Apple)


demo效果图

NCSignInWithApple

iOS 13后App Store审核指南有所更新

Sign In with Apple will be available for beta testing this summer. It will be required as an option for users in apps that support third-party sign-in when it is commercially available later this year.
当 Sign In with Apple 服务正式上线以后,所有已接入其它第三方登录的 App,Sign In with Apple 将被要求作为一种登录选择,否则有可能就不给过。
App Store审核指南更新

那么如何集成苹果授权登陆呢?

前期工作

需要在苹果后台打开该选项,并且重新生成Profiles配置文件
或者
在Xcode11 Signing & Capabilities中添加Sign In With Apple
证书会自动更新


Sign In With Apples设置

集成

1.下载NCSignInWithApple
导入文件夹NCSignInWithApple到工程

2.实现下面的代码

[HandleSignInWithAppleModel signInWithAppleWithButtonRect:CGRectMake(30, self.view.bounds.size.height - 180, self.view.bounds.size.width - 60, 100) withSupView:self.view withType:ASAuthorizationAppleIDButtonTypeDefault withStyle:ASAuthorizationAppleIDButtonStyleWhite success:^(ASAuthorization * _Nonnull authorization, NSString * _Nonnull user) {
        NSLog(@"授权成功");
    } failure:^(NSError * _Nonnull err) {
        NSLog(@"授权失败");
    }];

具体实现原理

实现流程
WX20191017-111909@2x.png
1. 添加Sign In with Apple登录按钮,设置ASAuthorizationAppleIDButton相关布局,并添加按钮点击响应事件。当然苹果也允许自定义苹果登录按钮的样式,样式要求详见这个文档:Human Interface Guidelines
2. 获取授权码

获取授权码需要在代码中实现两个代理回调ASAuthorizationControllerDelegate、ASAuthorizationControllerPresentationContextProviding分别用于处理授权登录成功和失败、以及提供用于展示授权页面的Window

在授权登录成功回调中,我们可以拿到以下几类数据

3. 验证

关于验证的这一步,需要传递授权码给自己的服务端,自己的服务端调用苹果API去校验授权码Generate and validate tokens。如果验证成功,可以根据userIdentifier判断账号是否已存在,若存在,则返回自己账号系统的登录态,若不存在,则创建一个新的账号,并返回对应的登录状态给App

附:官方示例代码 Swift 版
附:What the Heck is Sign In with Apple?
附:苹果授权登陆后端验证
附:App Store审核指南的更新
附:Generate and validate tokens
附:NCSignInWithApple

上一篇 下一篇

猜你喜欢

热点阅读