You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.8 KiB

//
// TFAppleSignManager.m
// WXReader
//
// Created by 谢腾飞 on 2020/12/4.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFAppleSignManager.h"
#import <AuthenticationServices/AuthenticationServices.h>
#import "AppDelegate.h"
@interface TFAppleSignManager ()<ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
@end
@implementation TFAppleSignManager
implementation_singleton(TFAppleSignManager)
- (void)tunedUpAppleSignWithState:(TFAppleSignState)state
{
ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
ASAuthorizationAppleIDRequest *request = [provider createRequest];
request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
ASAuthorizationController *authorization = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
authorization.delegate = self;
authorization.presentationContextProvider = self;
[authorization performRequests];
}
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0))
{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
return app.window;
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0))
{
if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]]) {
[self requestTouristsLogin];
}
}
// 游客登录请求
- (void)requestTouristsLogin
{
[TFPromptManager showPromptViewWithStatus:TFPromptStatusLoading promptTitle:TFLocalizedString(@"正在登录")];
WS(weakSelf)
[TFNetworkTools POST:Tourists_Login parameters:@{@"udid":[TFUtilsHelper getUDID]} model:TFUserInfoManager.class success:^(BOOL isSuccess, TFUserInfoManager * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) {
[TFPromptManager hiddenAlert];
if (isSuccess) {
[weakSelf appleSignResponseSuccess:t_model];
} else {
[weakSelf appleSignResponseFail:requestModel.msg];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[TFPromptManager hiddenAlert];
[weakSelf appleSignResponseFail:error.localizedFailureReason ?: TFLocalizedString(@"登录失败")];
}];
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0))
{
NSString *errorMsg = nil;
switch (error.code) {
case ASAuthorizationErrorCanceled:
errorMsg = TFLocalizedString(@"您已取消授权");
break;
case ASAuthorizationErrorFailed:
errorMsg = TFLocalizedString(@"授权请求失败");
break;
case ASAuthorizationErrorInvalidResponse:
errorMsg = TFLocalizedString(@"授权请求响应无效");
break;
case ASAuthorizationErrorNotHandled:
errorMsg = TFLocalizedString(@"未能处理授权请求");
break;
case ASAuthorizationErrorUnknown:
errorMsg = TFLocalizedString(@"授权失败");
break;
}
[TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:errorMsg];
}
- (void)appleSignResponseSuccess:(TFUserInfoManager *)userData
{
if (self.delegate && [self.delegate respondsToSelector:@selector(appleSignResponseSuccess:)]) {
[self.delegate appleSignResponseSuccess:userData];
}
}
- (void)appleSignResponseFail:(NSString *)error
{
if (self.delegate && [self.delegate respondsToSelector:@selector(appleSignResponseFail:)]) {
[self.delegate appleSignResponseFail:error ? : @""];
}
}
@end