apple login

This commit is contained in:
2025-10-30 14:29:11 +08:00
parent f58bf61500
commit 85a3694e35
7 changed files with 388 additions and 6 deletions

View File

@@ -0,0 +1,154 @@
// LoginViewController.m
#import "LoginViewController.h"
#import "AppleSignInManager.h"
#import <AuthenticationServices/AuthenticationServices.h>
#import <Masonry/Masonry.h>
@interface LoginViewController ()
//
@property (nonatomic, strong) UIView *contentView;
//
@property (nonatomic, strong) UILabel *titleLabel;
// Apple
@property (nonatomic, strong) UIView *appleContainer;
// iOS13+ Apple
@property (nonatomic, strong) ASAuthorizationAppleIDButton *appleButton API_AVAILABLE(ios(13.0));
// iOS13
@property (nonatomic, strong) UIButton *compatHintButton;
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (@available(iOS 13.0, *)) {
self.view.backgroundColor = [UIColor systemBackgroundColor];
} else {
self.view.backgroundColor = [UIColor whiteColor];
}
[self setupUI]; //
}
- (void)setupUI {
// contentView
[self.view addSubview:self.contentView];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.left.equalTo(self.view).offset(24);
make.right.equalTo(self.view).offset(-24);
}];
// titleLabel
[self.contentView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.contentView);
}];
// appleContainer
[self.contentView addSubview:self.appleContainer];
[self.appleContainer mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom).offset(24);
make.left.right.equalTo(self.contentView);
make.height.mas_equalTo(50);
make.bottom.equalTo(self.contentView);
}];
}
//
- (UIView *)contentView {
if (!_contentView) {
_contentView = [UIView new];
}
return _contentView;
}
//
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel new];
_titleLabel.text = @"登录";
_titleLabel.font = [UIFont boldSystemFontOfSize:24];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
// Apple
- (UIView *)appleContainer {
if (!_appleContainer) {
_appleContainer = [UIView new];
if (@available(iOS 13.0, *)) {
[_appleContainer addSubview:self.appleButton];
[self.appleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_appleContainer);
}];
} else {
[_appleContainer addSubview:self.compatHintButton];
[self.compatHintButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_appleContainer);
}];
}
}
return _appleContainer;
}
// Apple iOS13+
- (ASAuthorizationAppleIDButton *)appleButton API_AVAILABLE(ios(13.0)) {
if (!_appleButton) {
_appleButton = [ASAuthorizationAppleIDButton new];
[_appleButton addTarget:self action:@selector(handleAppleIDButtonPress) forControlEvents:UIControlEventTouchUpInside];
}
return _appleButton;
}
// iOS13
- (UIButton *)compatHintButton {
if (!_compatHintButton) {
_compatHintButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_compatHintButton setTitle:@"需要 iOS13+ 才能使用 Apple 登录" forState:UIControlStateNormal];
_compatHintButton.enabled = NO;
}
return _compatHintButton;
}
- (void)handleAppleIDButtonPress API_AVAILABLE(ios(13.0)) {
__weak typeof(self) weakSelf = self;
[[AppleSignInManager shared] signInFromViewController:self completion:^(ASAuthorizationAppleIDCredential * _Nullable credential, NSError * _Nullable error) {
__strong typeof(weakSelf) selfStrong = weakSelf;
if (error) {
if (selfStrong.onLoginFailure) selfStrong.onLoginFailure(error);
return;
}
if (!credential) return;
NSString *userID = credential.user ?: @"";
NSString *email = credential.email ?: @""; //
NSPersonNameComponents *name = credential.fullName; //
NSString *givenName = name.givenName ?: @"";
NSString *familyName = name.familyName ?: @"";
NSMutableDictionary *info = [@{ @"userIdentifier": userID } mutableCopy];
if (email.length) info[@"email"] = email;
if (givenName.length || familyName.length) {
info[@"givenName"] = givenName;
info[@"familyName"] = familyName;
}
// identityToken / authorizationCode
NSData *tokenData = credential.identityToken;
if (tokenData) {
NSString *token = [[NSString alloc] initWithData:tokenData encoding:NSUTF8StringEncoding] ?: @"";
if (token.length) info[@"identityToken"] = token;
}
NSData *codeData = credential.authorizationCode;
if (codeData) {
NSString *code = [[NSString alloc] initWithData:codeData encoding:NSUTF8StringEncoding] ?: @"";
if (code.length) info[@"authorizationCode"] = code;
}
if (selfStrong.onLoginSuccess) selfStrong.onLoginSuccess(info);
}];
}
@end