good测试(实践出真知)

iOS Apple登录

2020-03-18  本文已影响0人  winsonGali
添加依赖库
AuthenticationServices.framework
导入头文件
#import <AuthenticationServices/AuthenticationServices.h>
遵循代理
<ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
创建 appleIDButton
if (@available(iOS 13.0, *)) {
    ASAuthorizationAppleIDButton *abtn = [ASAuthorizationAppleIDButton
buttonWithType:ASAuthorizationAppleIDButtonTypeSignIn
style:ASAuthorizationAppleIDButtonStyleBlack];
    [abtn addTarget:self action:@selector(signInWithApple)
forControlEvents:UIControlEventTouchUpInside];
    abtn.cornerRadius = 25;
    NSLog(@"abtn = %@", NSStringFromCGRect(abtn.frame));
    [self.appleIDView addSubview:abtn];
}
发起请求
- (void)signInWithApple API_AVAILABLE(ios(13.0)) {
    NSLog(@"使用苹果登录");
    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];
}
苹果返回登录信息
#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;
        //NSLog(@"授权返回信息:%@", credential.mj_JSONString);       
        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;
}


苹果登录返回信息
识别用户
    identityToken:JSON Web令牌(JWT),可将有关用户的信息安全地传递到您的应用程序。
    authorizationCode:与应用程序的服务器副本交互时,应用程序使用的短暂令牌来作为授权证明。
    state:您的应用程序提供给生成凭据的请求的任意字符串。
    user:与已认证用户关联的标识符。(苹果用户唯一标识符)
获取用户信息
    authorizedScopes:用户授权您的应用访问的联系信息。
    fullName:名称对象
    email:用户邮箱地址(真实或者隐藏)
检测用户特征
    realUserStatus:指示用户是否看起来是真实人物的值。
    ASUserDetectionStatus:实际用户指标的可能值



一、共享我的邮箱
state: (null)
userID: 001324.bd4cdc4453964f23859a106528dc7b00.0257
fullName: <NSPersonNameComponents: 0x60000046dc10> {givenName = 永胜, familyName = 刘, middleName = (null), namePrefix = (null), nameSuffix = (null), nickname = (null) phoneticRepresentation = (null) }
email: 1595546214@qq.com
authorizationCode: c7539fdca92bb4b78b4b8e54a128b34d9.0.nrtsu.fOYwoxw-PWeSfXyWnXseCg
identityToken: eyJraWQiOiJBSURPUEsxIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLmd1bGkuemVuYm9ybiIsImV4cCI6MTU3MzAwOTg5OCwiaWF0IjoxNTczMDA5Mjk4LCJzdWIiOiIwMDEzMjQuYmQ0Y2RjNDQ1Mzk2NGYyMzg1OWExMDY1MjhkYzdiMDAuMDI1NyIsImNfaGFzaCI6ImF5aWhyR0RaNndRZkdra3NFM3hhOGciLCJlbWFpbCI6IjE1OTU1NDYyMTRAcXEuY29tIiwiZW1haWxfdmVyaWZpZWQiOiJ0cnVlIiwiYXV0aF90aW1lIjoxNTczMDA5Mjk4fQ.DS41gpURwyowq9bXCmYni_p6KQq7NmBn_uLKA_Kymz8AS3V4rxcgKk8NoVuqPIIbW-GkUQjhzF1tWICNhqTnAoJgSIuDxDYO0xubPg4jwgr1ZgqtDUix-l6evBfuR-7DSbL2E3Rl7T1VFxX-MWL_nJDcfuh3wlOUbTE_RfbgPMwv4V4HPTskFa9kWS9-NMUvs9PUGiAkMSUx6AnqCZ65nbLqq13h0pMSaX8NFgUvZrLyonbgDU8cMkVwz_X3CMQGF1UHSkRiMlCeoYU0Z-2w6RGAhMPHCGZZWx48aefO8obn_NugfXbtWFsKBKTaJ09M0CBQP6679_NRb2n-0mXEXw
realUserStatus: 1


二、隐藏我的邮箱
state: (null)
userID: 001324.bd4cdc4453964f23859a106528dc7b00.0257
fullName: <NSPersonNameComponents: 0x600000465cd0> {givenName = Lau, familyName = Winson, middleName = (null), namePrefix = (null), nameSuffix = (null), nickname = (null) phoneticRepresentation = (null) }
email: 6ydppeishc@privaterelay.appleid.com
authorizationCode: cb7a915f207d9462886eea8711c87cb96.0.nrtsu.JHIFsv-gkeb3MkEdRQwmNA
identityToken: eyJraWQiOiJBSURPUEsxIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLmd1bGkuemVuYm9ybiIsImV4cCI6MTU3MzAxMDMxOCwiaWF0IjoxNTczMDA5NzE4LCJzdWIiOiIwMDEzMjQuYmQ0Y2RjNDQ1Mzk2NGYyMzg1OWExMDY1MjhkYzdiMDAuMDI1NyIsImNfaGFzaCI6IjdQYkF5N3VVMk5XS2VoNFVzV3lfQkEiLCJlbWFpbCI6IjZ5ZHBwZWlzaGNAcHJpdmF0ZXJlbGF5LmFwcGxlaWQuY29tIiwiZW1haWxfdmVyaWZpZWQiOiJ0cnVlIiwiaXNfcHJpdmF0ZV9lbWFpbCI6InRydWUiLCJhdXRoX3RpbWUiOjE1NzMwMDk3MTh9.J67iq7rjgqJZBxkryzGcZnIK2-Zsyab85QrhlhPDjfY2N9cZN0zlNWkiNlZB6oDbGYOU9Gjh20phh3SSBrHNfixNpRD-kypSqPPu9E84qdnUjX6EypKX27vx0a2fM11jByEV05A_KU7fKaKz0F5TZZ40wk6MibaKHuj4e5r2mQOA7Bgkr_uiygeigqWYACE0wo1Did2rOY40M6mWYOYvg0ALWifzXe1c0V_rKHQFpfLpgX1lty4GnsWtyglLESCNeIoWhRnhxK_5BESGKrSUbzIROTx3kd9dGT1OWcAu2HyyU-xsELPS1iPqdZix18CzY-4QwkB7Uhyo6jbBP9mlpA
realUserStatus: 1
上一篇下一篇

猜你喜欢

热点阅读