Commit 87bfdd09 authored by 汪洋's avatar 汪洋

Merge branch 'jql/appleLogin' into 'master'

添加苹果登录

See merge request !3
parents 18b8bdd4 25d4872f
......@@ -39,6 +39,7 @@ typedef NS_ENUM(NSInteger, GMSharePlatform) {
GMSharePlatformWechat = 100, /**< 微信平台*/
GMSharePlatformQQ = 101, /**< QQ平台*/
GMSharePlatformDouyin = 102, /**< 抖音*/
GMSharePlatformApple = 103 /**苹果登录*/
};
......@@ -103,6 +104,7 @@ typedef NS_ENUM(NSUInteger, GMShareContentType){
#define kQQLogin @"kQQLogin"
#define kSinaLogin @"kSinaLogin"
#define kAppleLogin @"kAppleLogin"
#define kWechatLogin @"kWechatLogin"
#define kWechatPaySuccess @"kWechatPaySuccess"
#define kWechatPayFail @"kWechatPayFail"
......
......@@ -18,6 +18,8 @@
@property (nonatomic, copy) NSString * city;
@property (nonatomic, assign) GMSharePlatform platformType;
@property (nonatomic, copy) NSString *thirdDetail;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *authorizationCode;
@end
......
......@@ -53,6 +53,31 @@
userHandler(state, note.object);
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}];
} else if (platform == GMSharePlatformApple) {
if (@available(iOS 13.0, *)) {
// 基于用户的Apple ID授权用户,生成用户授权请求的一种机制
ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
// 创建新的AppleID 授权请求
ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
// 在用户授权期间请求的联系信息
appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
// 由ASAuthorizationAppleIDProvider创建的授权请求 管理授权请求的控制器
ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
// 设置授权控制器通知授权请求的成功与失败的代理
authorizationController.delegate = [GMThirdPartyOauthManager shareInstance];
// 设置提供 展示上下文的代理,在这个上下文中 系统可以展示授权界面给用户
authorizationController.presentationContextProvider = [GMThirdPartyOauthManager shareInstance];
// 在控制器初始化期间启动授权流
[authorizationController performRequests];
__block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:kAppleLogin object:nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
GMShareResponseState state = (GMShareResponseState )[note.userInfo[@"state"] integerValue];
userHandler(state, note.object);
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}];
} else {
userHandler(GMShareResponseStateFail, nil);
}
}
}
......
......@@ -13,6 +13,7 @@
#import <WechatOpenSDK/WXApi.h>
#import <Weibo_SDK/WeiboSDK.h>
#import <DouyinOpenSDK/DouyinOpenSDKApi.h>
#import <AuthenticationServices/AuthenticationServices.h>
//这里是接受三方授权的回调
@interface GMThirdPartyOauthManager : NSObject <TencentSessionDelegate, QQApiInterfaceDelegate, WXApiDelegate, WeiboSDKDelegate, DouyinOpenSDKApiDelegate>
......
......@@ -13,7 +13,7 @@
@interface GMThirdPartyOauthManager()<WBHttpRequestDelegate, DouyinOpenSDKLogDelegate>
@interface GMThirdPartyOauthManager()<WBHttpRequestDelegate, DouyinOpenSDKLogDelegate, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
@end
@implementation GMThirdPartyOauthManager
......@@ -46,6 +46,8 @@
[[NSNotificationCenter defaultCenter] postNotificationName:kSinaLogin object:nil userInfo:@{@"state": @(GMShareResponseStateBack)}];
} else if (_user.platformType == GMSharePlatformWechat){
[[NSNotificationCenter defaultCenter] postNotificationName:kWechatLogin object:nil userInfo:@{@"state": @(GMShareResponseStateBack)}];
} else if (_user.platformType == GMSharePlatformApple){
[[NSNotificationCenter defaultCenter] postNotificationName:kAppleLogin object:nil userInfo:@{@"state": @(GMShareResponseStateBack)}];
}
}
}
......@@ -290,6 +292,47 @@
}
}
#pragma mark - Apple delegate
//@optional 授权成功地回调
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)){
if (@available(iOS 13.0, *)) {
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
// 用户登录使用ASAuthorizationAppleIDCredential
ASAuthorizationAppleIDCredential *appleIDCredential = (ASAuthorizationAppleIDCredential *)authorization.credential;
NSString *user = appleIDCredential.user;
// 使用过授权的,可能获取不到以下三个参数
NSString *fimilyName = appleIDCredential.fullName.familyName ?: @"";
NSString *givenName = appleIDCredential.fullName.givenName ?: @"";
NSData *identityToken = appleIDCredential.identityToken;
NSData *authorizationCode = appleIDCredential.authorizationCode;
_user.uid = appleIDCredential.user;
_user.nickname = [fimilyName stringByAppendingString:givenName];
_user.email = appleIDCredential.email;
_user.thirdDetail = [[NSString alloc] initWithData:identityToken encoding:NSUTF8StringEncoding];
_user.authorizationCode = [[NSString alloc] initWithData:authorizationCode encoding:NSUTF8StringEncoding];
_user.platformType = GMSharePlatformApple;
[[NSNotificationCenter defaultCenter] postNotificationName:kAppleLogin object:_user userInfo:@{@"state": @(GMShareResponseStateSuccess)}];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:kAppleLogin object:nil userInfo:@{@"state": @(GMShareResponseStateFail)}];
}
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:kAppleLogin object:nil userInfo:@{@"state": @(GMShareResponseStateFail)}];
}
}
// 授权失败的回调
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)){
[[NSNotificationCenter defaultCenter] postNotificationName:kAppleLogin object:nil userInfo:@{@"state": @(error.code == ASAuthorizationErrorCanceled ? GMShareResponseStateCancel : GMShareResponseStateFail)}];
}
// 告诉代理应该在哪个window 展示内容给用户
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)){
// 返回window
return [UIApplication sharedApplication].windows.lastObject;
}
- (void)onLog:(NSString *)log {
NSLog(@"%@",log);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment