iOS实现苹果第三方登录功能:Sign in with Apple

学而时习之,不亦乐乎,大家好,我是张杰。

上半年苹果登录可谓争论不休,至今也有不少疑问。今天亲测给大家讲讲。

网上两个观点:

1、只要项目里面用到了三方登录就一定要加上苹果登录。
2、项目里面要是有自己的登录方式(比如手机号登录)加上其他三方登录可以不用苹果登录。

下面是苹果官网给的解释:
符合以下条件的 App,可以不接入:

使用自建账户和登录系统;
要求用户使用现有的教育或企业账户登录的教育、企业或商业类应用;
使用政府或行业支持的公民身份识别系统或电子 ID 来验证用户;
应用特定于第三方服务,用户需要使用邮箱、社交媒体或其它第三方账户才能访问其内容的应用。
直通车:https://developer.apple.com/cn/news/?id=09122019b

反正理解的不是很透彻,结合市面上大部分APP,我得出一下结论:

只要用到了微信登录,QQ登录,新浪登录、微博登录等主流三方登录,你就要加上苹果登录!!!!

下面是我提交APP的界面:

iOS实现苹果第三方登录功能:Sign in with Apple_第1张图片
681604540431_.pic.jpg

然后苹果审核被拒信息:

iOS实现苹果第三方登录功能:Sign in with Apple_第2张图片
671604540147_.pic.jpg

然后就进行了改进:


iOS实现苹果第三方登录功能:Sign in with Apple_第3张图片
691604540438_.pic.jpg

下面讲具体怎么做还有需要注意的点。

一、苹果登录流程

iOS实现苹果第三方登录功能:Sign in with Apple_第4张图片
701604541368_.pic_hd.jpg

二、项目设置

1、进入 https://developer.apple.com 勾上Sign in with Apple

iOS实现苹果第三方登录功能:Sign in with Apple_第5张图片
631604280989_.pic_hd.jpg

2、重新生成profile文件,替换项目里面的profile文件

3、项目里面添加 Sign in with Apple

iOS实现苹果第三方登录功能:Sign in with Apple_第6张图片
711604541855_.pic_hd.jpg

三、代码设置

1、自己添加按钮或者使用苹果自己的ASAuthorizationAppleIDButton

if (@available(iOS 13.0, *)) {
            ASAuthorizationAppleIDButton *appleLoginBtn = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeSignIn authorizationButtonStyle:ASAuthorizationAppleIDButtonStyleBlack];
            appleLoginBtn.frame = CGRectMake(0, 0, 50, 50);
            appleLoginBtn.layer.cornerRadius = 5;
            appleLoginBtn.layer.masksToBounds = YES;
            [appleLoginBtn addTarget:self action:@selector(appleLogin) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:appleLoginBtn];
        } else {
            // Fallback on earlier versions
        }

这里放个直通车:https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple/overview/buttons/苹果按钮的大小,黑白等都要按照要求来,不然还可能被拒。

2、导入头文件,设置代理

#import 


@interface LoginViewController ()

3、按钮触发,拉起苹果登录代码

if (@available(iOS 13.0, *)) {
    ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
    // 通过 provider 创建一个 request
    ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
    // 要获取的内容
    appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
    // 系统提供的 Controller,必须使用,需要传入 requests 数组
    ASAuthorizationController *authController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest/*, passwordRequest*/]];
    // 设置代理,接收登录成功/失败的回调
    authController.delegate = self;
    // 页面跳转相关的,通过一个代理方法传入一个 window
    authController.presentationContextProvider = self;

    [authController performRequests];
}

4、代理方法

///代理主要用于展示在哪里
-(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
return self.view.window;
}


-(void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
    if([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]){
        ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
                NSString *user = appleIDCredential.user;
                // 使用过授权的,可能获取不到以下三个参数
                NSString *familyName = appleIDCredential.fullName.familyName;
                NSString *givenName = appleIDCredential.fullName.givenName;
                NSString *email = appleIDCredential.email;
                
                NSData *identityToken = appleIDCredential.identityToken;
                NSData *authorizationCode = appleIDCredential.authorizationCode;
                
                // 服务器验证需要使用的参数
                NSString *identityTokenStr = [[NSString alloc] initWithData:identityToken encoding:NSUTF8StringEncoding];
                NSString *authorizationCodeStr = [[NSString alloc] initWithData:authorizationCode encoding:NSUTF8StringEncoding];
                NSLog(@"%@\n\n%@", identityTokenStr, authorizationCodeStr);
        //这里后台需要什么就传什么给他
        
    }else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]){
        
        //// Sign in using an existing iCloud Keychain credential.
        ASPasswordCredential *pass = authorization.credential;
        NSString *username = pass.user;
        NSString *passw = pass.password;
        
    }

}

至此,你已经可以拉起苹果登录了,后面就是你自己去做你的业务了。

参考:
https://www.jianshu.com/p/d3d084f28dcb
http://www.woshipm.com/pd/3500359.html

如果有错误或者还有其他问题,可以联系我:[email protected],谢谢

你可能感兴趣的:(iOS实现苹果第三方登录功能:Sign in with Apple)