iOS三方登录

第三方登录 Sign in with Apple

2020-05-18  本文已影响0人  小涛_78e8

首先需要登录开发者中心,将Identifiers打开Sign in with Apple权限,然后从新更新描述文件Profiles。


image.png

然后Xcode在这里添加Sign in with Apple,再然后就可以去敲代码了。


image.png
导入头文件#import <AuthenticationServices/AuthenticationServices.h>,再加上代理ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding
//系统提供的按钮(如果用系统提供按钮,建议第三方登录做成这样,别问为什么,因为被拒的多了)
image.png

if (@available(iOS 13.0, *)) {
// Sign In With Apple Button
ASAuthorizationAppleIDButton *appleIDBtn = [ASAuthorizationAppleIDButton buttonWithType:ASAuthorizationAppleIDButtonTypeDefault style:ASAuthorizationAppleIDButtonStyleWhite];
[self.view addSubview:appleIDBtn];
[appleIDBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(200);
make.height.mas_equalTo(50);
make.bottom.equalTo(line.mas_top).offset(-20);
make.centerX.equalTo(line);
}];
appleIDBtn.layer.cornerRadius = 25;
appleIDBtn.layer.borderWidth = 1;
appleIDBtn.layer.borderColor = [UIColor grayColor].CGColor;
[appleIDBtn addTarget:self action:@selector(didAppleIDBtnClicked) forControlEvents:UIControlEventTouchUpInside];

}

或者选择自定义,图片样式建议这样,或者圆角、矩形。


image.png

苹果官方给出样式


image.png

// ASAuthorizationPasswordRequest *passwordRequest = [[ASAuthorizationPasswordProvider new] createRequest];

NSMutableArray <ASAuthorizationRequest *>* array = [NSMutableArray arrayWithCapacity:2];
if (authAppleIDRequest) {
    [array addObject:authAppleIDRequest];
}

// if (passwordRequest) {
// [array addObject:passwordRequest];
// }
NSArray <ASAuthorizationRequest > requests = [array copy];

// 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:requests];
authorizationController.delegate = self;
// 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
authorizationController.presentationContextProvider = self;

// 在控制器初始化期间启动授权流
[authorizationController performRequests];
}

//这个值试了多次发现没有变
NSStringuser = appleIDCredential.user;
//下面4个值只有第一次能获取到,以后每次获取的都是空值
NSString
familyName = appleIDCredential.fullName.familyName;
NSString*nickname = appleIDCredential.fullName.nickname;

    NSString*givenName = appleIDCredential.fullName.givenName;

    NSString*email = appleIDCredential.email;

//下面2个每次都会变
NSString *authorizationCode = [[NSString alloc] initWithData:appleIDCredential.authorizationCode encoding:NSUTF8StringEncoding]; // refresh token

    NSString *identityToken = [[NSString alloc] initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding]; // access token

   //自己用哪个值,根据实际情况来定,也可以将 identityToken存钥匙串保证唯一性。

}else if([authorization.credential isKindOfClass:[ASPasswordCredential class]]){

    // Sign in using an existing iCloud Keychain credential.

    // 用户登录使用现有的密码凭证

    ASPasswordCredential*passwordCredential = authorization.credential;

    // 密码凭证对象的用户标识 用户的唯一标识

    NSString*user = passwordCredential.user;

    // 密码凭证对象的密码

    NSString*password = passwordCredential.password;

    [mStr appendString:user];

    [mStr appendString:@"\n"];

    [mStr appendString:password];

    [mStr appendString:@"\n"];

    NSLog(@"mStr:::%@", mStr);

}else{

    NSLog(@"授权信息均不符");

    mStr = [@"授权信息均不符"copy];

}

}

// 授权失败的回调

}

上一篇 下一篇

猜你喜欢

热点阅读