• 主页
  • 随笔
  • 技术笔记
  • 全部文章
标签 友链 关于我

  • 主页
  • 随笔
  • 技术笔记
  • 全部文章

Apple授权登录-sign in with Apple

阅读数:次 2022-05-01
字数统计: 976字   |   阅读时长≈ 4分

霸王条款规定,如果你的App中包含或涉及到第三方登录(例如:QQ登陆、微信登陆等),就必须接入苹果登陆,如果不适配苹果推出的Sign In With Apple审核将会被拒绝。

一、授权Apple登录(整体交互过程)

其实和平时的一些第三方登陆一样,通过授权,可以拿到用户名,邮箱地址,用户ID等信息,接入 Sign In With Apple 后, 应用程序或网站中显示“ 通过Apple登录”按钮,意味着可以使用他们已有的Apple ID进行点击或登录,而无需填写表格,验证电子邮件地址和选择密码等操作;提供了一种新的,更私密的方式登陆。

二、配置开发证书权限

1、首先手机必须是iOS 13(包含iOS 13)以上的iOS系统

2、项目 Bundle ID(Bundle identifier)勾选 Sign In With Apple 功能,然后保存

三、Xcode 配置 Singing & Capabilities

注意:(Debug 和 Release)模式都要配置,否则打出正式包不能调出授权弹窗 ,亲身经历!!!!

四、添加依赖库

五、代码集成

1.引入头文件

1
#import <AuthenticationServices/AuthenticationServices.h> 

2.添加代理

1
<ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>

3.添加“ 通过Apple登录”按钮 ,必须用系统的按钮样式

1
2
3
4
5
6
7
8
9
10
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDButton *abtn = [ASAuthorizationAppleIDButton
buttonWithType:ASAuthorizationAppleIDButtonTypeSignIn
style:ASAuthorizationAppleIDButtonStyleBlack];
[abtn addTarget:self action:@selector(signInWithApple) forControlEvents:UIControlEventTouchUpInside];
// 圆角设置
// abtn.cornerRadius = 0;
abtn.frame = CGRectMake((ScreenWidth - 200)/2, (ScreenHeight - 100)/2, 200, 40);
[self.view addSubview:abtn];
}

4.点击“ 通过Apple登录”按钮执行方法

1
2
3
4
5
6
7
8
9
if (@available(iOS 13.0, *)) {
ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest *request = [provider createRequest];
request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
ASAuthorizationController *vc = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
vc.delegate = self;
vc.presentationContextProvider = self;
[vc performRequests];
}

5.授权的代理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma mark - ASAuthorizationControllerDelegate
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
NSString *errorMsg = nil;
switch (error.code) {
case ASAuthorizationErrorCanceled:
errorMsg = @"用户取消了授权请求";
break;
case ASAuthorizationErrorFailed:
errorMsg = @"授权请求失败";
break;
case ASAuthorizationErrorInvalidResponse:
errorMsg = @"授权请求响应无效";
break;
case ASAuthorizationErrorNotHandled:
errorMsg = @"未能处理授权请求";
break;
case ASAuthorizationErrorUnknown:
errorMsg = @"授权请求失败未知原因";
break;
}
NSLog(@"%@", errorMsg);
}

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {

if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
ASAuthorizationAppleIDCredential *credential = (ASAuthorizationAppleIDCredential *)authorization.credential;
NSString *state = credential.state;
NSString *userID = credential.user;
NSPersonNameComponents *fullName = credential.fullName;
NSString *email = credential.email;
NSString *authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding];
NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding];
ASUserDetectionStatus realUserStatus = credential.realUserStatus;
NSArray *authorizedScopes = credential.authorizedScopes;

NSLog(@"state: %@", state);
NSLog(@"userID: %@", userID);
NSLog(@"fullName: %@", fullName);
NSLog(@"email: %@", email);
NSLog(@"authorizationCode: %@", authorizationCode);
NSLog(@"identityToken: %@", identityToken);
NSLog(@"realUserStatus: %@", @(realUserStatus));
NSLog(@"authorizedScopes: %@", authorizedScopes);
}
}

#pragma mark - ASAuthorizationControllerPresentationContextProviding
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)) {
return [UIApplication sharedApplication].keyWindow;
}
- (NSString *)documentPath{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
return documentPath;
}

6.版本支持

苹果授权登录是iOS13之后出现的新功能,所以在用这个功能的时候,需要进行版本检查。一般类似这样:

1
2
3
4
5
6
7
if (@available(iOS 13.0, *)) {
// 授权登录操作
[weakSelf sendAppleAuthRequest];
} else {
// 低版本,给出提示
[XLToast showErrorMessage:@"当前系统版本不支持apple登录"];
}

六.注意事项

1.首次登录会返回所有参数,包括全名、邮箱等(如果玩家登录时拒绝提供真实的邮箱账号,苹果会生成虚拟的邮箱账号),二次登录只会返回 UserID 和 授权码,其它信息不再返回!

2.同一个开发者账号下所有APP,同一个apple id登陆时,获取的UserID 是一样的。

3.两个开发者账号下的应用,同一个apple id登陆时,获取的 UserID 不一样

4.NSNotification.Name.ASAuthorizationAppleIDProviderCredentialRevoked, 系统在苹果账号登出时通知,此时应用如果是苹果登陆的用户,有没有必要也登出账号?

5.如果打开登录界面时,设备的 iCloud Keychain 有 apple id 账号可用时,可以弹窗让用户选择 iCloud Keychain 里的账号来进行登陆。

七.运行效果

  • 本文作者: Grx
  • 本文链接: https://ruixiaoguo.github.io/Grx.github.io/Grx.github.io/2022/05/01/Apple授权登录-sign in with Apple/
  • 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!
  • OC

扫一扫,分享到微信

ios防crash崩溃框架-LSSafeProtector
微信支付宝离线支付原理
  1. 1. 一、授权Apple登录(整体交互过程)
    1. 1.0.1. 其实和平时的一些第三方登陆一样,通过授权,可以拿到用户名,邮箱地址,用户ID等信息,接入 Sign In With Apple 后, 应用程序或网站中显示“ 通过Apple登录”按钮,意味着可以使用他们已有的Apple ID进行点击或登录,而无需填写表格,验证电子邮件地址和选择密码等操作;提供了一种新的,更私密的方式登陆。
  • 2. 二、配置开发证书权限
    1. 2.0.1. 1、首先手机必须是iOS 13(包含iOS 13)以上的iOS系统
    2. 2.0.2. 2、项目 Bundle ID(Bundle identifier)勾选 Sign In With Apple 功能,然后保存
  • 3. 三、Xcode 配置 Singing & Capabilities
    1. 3.0.1. 注意:(Debug 和 Release)模式都要配置,否则打出正式包不能调出授权弹窗 ,亲身经历!!!!
  • 4. 四、添加依赖库
  • 5. 五、代码集成
    1. 5.0.1. 1.引入头文件
    2. 5.0.2. 2.添加代理
    3. 5.0.3. 3.添加“ 通过Apple登录”按钮 ,必须用系统的按钮样式
    4. 5.0.4. 4.点击“ 通过Apple登录”按钮执行方法
    5. 5.0.5. 5.授权的代理方法
    6. 5.0.6. 6.版本支持
  • 6. 六.注意事项
  • 7. 七.运行效果
  • © 2014-2024 Grx
    GitHub:hexo-theme-yilia-plus by Litten
    本站总访问量次 | 本站访客数人
    • 标签
    • 友链
    • 关于我

    tag:

    • life
    • OC
    • Google
    • Fastlane
    • Flutter
    • hexo
    • 智能家居
    • Apple Watch
    • 逆向
    • Lottie
    • PHP
    • cocos2d
    • Mac
    • MonkeyKing
    • RN
    • Swift
    • RAC
    • WKWebView
    • WebView
    • Xcode
    • xcode
    • ios
    • Android
    • appledoc
    • MMKV
    • LLVM
    • FreamWork






      
      

    • 唐巧的博客
    • 王巍(喵神)OneVsDen
    • 阿里“念纪“
    • 滴滴-戴铭
    • 郭曜源(ibireme)
    • 阿里”南栀倾寒“
    • 蘑菇街李忠
    • 码农人生
    • 玉令天下
    • bang
    • Ian的博客
    这里是Grx的个人博客:
    iOS开发工程师一枚
    联系方式:
    QQ:1217255509
    Email:grx0917@sina.com
    知识管理,时间管理,自我管理,架构即未来
    欢迎技术交流!