苹果登录

- (void)start {
    ASAuthorizationAppleIDProvider *appleIDProvider = [ASAuthorizationAppleIDProvider new];
        ASAuthorizationAppleIDRequest *request = appleIDProvider.createRequest;
        request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
        
        ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
        authorizationController.delegate = self;
        authorizationController.presentationContextProvider = self;
        [authorizationController performRequests];  
}
#pragma mark - ASAuthorizationController delegate
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization  API_AVAILABLE(ios(13.0)){
    if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
        ASAuthorizationAppleIDCredential *appleIDCredential = authorization.credential;
///保存下面这个用户信息用来后续验证用户是否还有效
        NSString *identifier = appleIDCredential.user;
        NSPersonNameComponents *fullName = appleIDCredential.fullName;
        NSString *email = appleIDCredential.email;
        NSString *authorizationCode = [[NSString alloc] initWithData:appleIDCredential.authorizationCode encoding:NSUTF8StringEncoding];
        NSString *token = [[NSString alloc] initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding];
        ///交付后台用作注册、登录
        
    } else if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {
        
    }
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error  API_AVAILABLE(ios(13.0)){
    switch (error.code) {
        case ASAuthorizationErrorUnknown: {
            
            break;
        }
        case ASAuthorizationErrorCanceled: {
            break;
        }
        case ASAuthorizationErrorInvalidResponse: {
            
            break;
        }
        case ASAuthorizationErrorNotHandled: {
            
            break;
        }
        case ASAuthorizationErrorFailed: {
            
            break;
        }
            
        default:
            break;
    }
}

#pragma mark - Section
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller  API_AVAILABLE(ios(13.0)){
    return self.window;
}
- (void)volidateAuthorizationAppleID {
    if (@available(iOS 13, *)) {
        ASAuthorizationAppleIDProvider *appleIDProvider = [ASAuthorizationAppleIDProvider new];
        NSString *user = xxx; ///授权时保存的user
        if (user.length) {
            [appleIDProvider getCredentialStateForUserID:user completion:^(ASAuthorizationAppleIDProviderCredentialState credentialState, NSError * _Nullable error) {
                switch (credentialState) {
                    case ASAuthorizationAppleIDProviderCredentialRevoked:
                    case ASAuthorizationAppleIDProviderCredentialNotFound:
                     {
                       ///苹果账号已经失效
                        break;
                    }
                    case ASAuthorizationAppleIDProviderCredentialAuthorized: {
                        break;
                    }
                    case ASAuthorizationAppleIDProviderCredentialTransferred:{
                        break;
                    }
                        
                    default:
                        break;
                }
            }];
        }
        
    } else {
        
    }
    
}

 

返回数据示例(用户姓名、邮箱信息只有第一次授权的时候能采集到,往后均不可获得,下面这些是第二次登录时获取的信息)

user
001977.a98624cc686e4bdb8521554ae4d9ea77.0652

authorizedScopes 数组为空

identityToken

eyJraWQiOiJlWGF1bm1MIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLm1pZGVhaW90Lm1hbGwiLCJleHAiOjE2MTI1OTQ3MzEsImlhdCI6MTYxMjUwvCMzMSwic3ViIjoiMDAxOTc3LmE5ODYyNGNjNjg2ZTRiZGI4MzIxNTU0YWU0ZDllYTc3LjA2NTIiLCJjX2hhc2giOiJvTmZvNVFzTTRnY1dKcU96REFNUHVBIiwiZW1haWwiOiI0Y2p3NnRxbnVjQHByaXZhdGVyZWxheS5hcHBsZWlkLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjoidHJ1ZSIsImlzX3ByaXZhdGVfZW1haWwiOiJ0cnVlIiwiYXV0aF90aW1lIjoxNjEyNTA4MzMxLCJub25jZV9zdXBwb3J0ZWQiOnRydWV9.ewYgu6yOrTaLnWWxyrq0zXvsFUVRbZ5slEG2Ua5-8Sl4U2fSDrCELTYmm_acwZBznE0cJjBiC3s9oqr-mm_l-5X0mlh5OSfNRN9v41TjAcx5hu8SNKdkmtOUr1D3irWOh4dJ-zn7uJyXpiiZCS6k5ZhoitWMUzKVUIXysLbBK1LH0ZyIq8alYaHRD9yn4092j2n8UVgF_aiu9c5mQWL2gsv8B1gWRsMu_Hj9vppXPMTg0S7Dl4G6ErfSwNtUcuXq15hZ3IhHvf5Zz7TTaBcZB3aJiibCOrDJQe7lO1enorPNJrJuWmzKygf_njxLIJ9AcPbptUEu6jAOxvi489V_-g

authorizationCode
cbcb5908c30c549da099479b8763b06c9.0.rrzxx.b1eQLW4bqSSfge98eLGzQg

 

关于与后台验证相关内容的文章

https://blog.csdn.net/pyf09/article/details/109100108   数据范例

https://blog.csdn.net/qq_45139677/article/details/109075401 后台java校验代码

posted @ 2020-05-14 16:47  雨筱逸悠  阅读(1053)  评论(0编辑  收藏  举报