2
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
#import "KBLoginSheetViewController.h"
|
||||
#import "AppleSignInManager.h"
|
||||
#import <objc/message.h>
|
||||
#import "KBULBridge.h" // Darwin 通知常量:用于通知扩展“UL已处理”
|
||||
#import "LSTPopView.h"
|
||||
#import "KBLoginPopView.h"
|
||||
|
||||
// 注意:用于判断系统已启用本输入法扩展的 bundle id 需与扩展 target 的
|
||||
// PRODUCT_BUNDLE_IDENTIFIER 完全一致。
|
||||
@@ -69,7 +72,14 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.loveKey.nyx.CustomK
|
||||
if ([host hasSuffix:@"app.tknb.net"]) {
|
||||
NSString *path = url.path.lowercaseString ?: @"";
|
||||
if ([path hasPrefix:@"/ul/settings"]) { [self kb_openAppSettings]; return YES; }
|
||||
if ([path hasPrefix:@"/ul/login"]) { [self kb_presentLoginSheetIfNeeded]; return YES; }
|
||||
if ([path hasPrefix:@"/ul/login"]) {
|
||||
// 通知扩展:UL 已被主 App 接收,扩展可取消回退到 Scheme
|
||||
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
|
||||
(__bridge CFStringRef)KBDarwinULHandled,
|
||||
NULL, NULL, true);
|
||||
[self kb_presentLoginSheetIfNeeded];
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
@@ -83,6 +93,10 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.loveKey.nyx.CustomK
|
||||
NSString *urlHost = url.host ?: @"";
|
||||
NSString *host = [urlHost lowercaseString];
|
||||
if ([host isEqualToString:@"login"]) { // kbkeyboard://login
|
||||
// 兜底路径:Scheme 也发一次“已处理”通知,避免扩展重复尝试
|
||||
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
|
||||
(__bridge CFStringRef)KBDarwinULHandled,
|
||||
NULL, NULL, true);
|
||||
[self kb_presentLoginSheetIfNeeded];
|
||||
return YES;
|
||||
} else if ([host isEqualToString:@"settings"]) { // kbkeyboard://settings
|
||||
@@ -100,7 +114,37 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.loveKey.nyx.CustomK
|
||||
if (loggedIn) return;
|
||||
UIViewController *top = [UIViewController kb_topMostViewController];
|
||||
if (!top) return;
|
||||
[KBLoginSheetViewController presentIfNeededFrom:top];
|
||||
// [KBLoginSheetViewController presentIfNeededFrom:top];
|
||||
[self goLogin];
|
||||
|
||||
}
|
||||
|
||||
- (void)goLogin{
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
KBLoginPopView *view = [[KBLoginPopView alloc] initWithFrame:CGRectMake(0, 0, KB_SCREEN_WIDTH, KB_SCREEN_WIDTH)];
|
||||
// 创建并弹出
|
||||
LSTPopView *pop = [LSTPopView initWithCustomView:view
|
||||
parentView:nil
|
||||
popStyle:LSTPopStyleSmoothFromBottom
|
||||
dismissStyle:LSTDismissStyleSmoothToBottom];
|
||||
pop.hemStyle = LSTHemStyleBottom;
|
||||
pop.bgColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
|
||||
pop.isClickBgDismiss = YES; // 点击背景关闭
|
||||
pop.cornerRadius = 0; // 自定义 view 自处理圆角
|
||||
|
||||
__weak typeof(pop) weakPop = pop;
|
||||
view.appleLoginHandler = ^{
|
||||
[weakPop dismiss];
|
||||
[[AppleSignInManager shared] signInFromViewController:KB_CURRENT_NAV completion:^(ASAuthorizationAppleIDCredential * _Nullable credential, NSError * _Nullable error) {
|
||||
NSLog(@"=====");
|
||||
}];
|
||||
};
|
||||
view.closeHandler = ^{ [weakPop dismiss]; };
|
||||
|
||||
[pop pop];
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (void)kb_openAppSettings {
|
||||
|
||||
28
keyBoard/Class/Login/V/KBLoginPopView.h
Normal file
28
keyBoard/Class/Login/V/KBLoginPopView.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// KBLoginPopView.h
|
||||
// 主 App 登录弹窗自定义视图(给 LSTPopView 使用)
|
||||
//
|
||||
// 说明:
|
||||
// - 仅负责展示 UI 与回调,不直接触发 Apple 登录;
|
||||
// - 外层通过 LSTPopView 承载,点击按钮后先关闭弹窗,再由外层触发 Apple 登录。
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface KBLoginPopView : UIView
|
||||
|
||||
/// 关闭按钮回调(外层收到后可调用 [pop dismiss])
|
||||
@property (nonatomic, copy, nullable) void (^closeHandler)(void);
|
||||
|
||||
/// 点击“用 Apple 登录”回调(外层收到后:先 dismiss 弹窗,再触发 Apple 登录)
|
||||
@property (nonatomic, copy, nullable) void (^appleLoginHandler)(void);
|
||||
|
||||
/// 指定宽度初始化(高度内部自适应)
|
||||
- (instancetype)initWithWidth:(CGFloat)width;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
143
keyBoard/Class/Login/V/KBLoginPopView.m
Normal file
143
keyBoard/Class/Login/V/KBLoginPopView.m
Normal file
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// KBLoginPopView.m
|
||||
// 自定义登录弹窗内容视图(配合 LSTPopView 使用)
|
||||
//
|
||||
// 要点:
|
||||
// - 使用 Masonry 进行布局,支持 iOS 13+ 的 ASAuthorizationAppleIDButton
|
||||
// - 中文注释,懒加载子视图
|
||||
//
|
||||
|
||||
#import "KBLoginPopView.h"
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <AuthenticationServices/AuthenticationServices.h>
|
||||
|
||||
@interface KBLoginPopView ()
|
||||
@property (nonatomic, strong) UILabel *titleLabel; // 标题
|
||||
@property (nonatomic, strong) UILabel *descLabel; // 副标题/说明
|
||||
@property (nonatomic, strong) UIButton *closeButton; // 右上角关闭
|
||||
@property (nonatomic, strong) UIView *appleContainer; // Apple 按钮容器(iOS13- 时显示占位)
|
||||
@end
|
||||
|
||||
@implementation KBLoginPopView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame{
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
self.backgroundColor = [UIColor whiteColor];
|
||||
self.layer.cornerRadius = 16.0;
|
||||
self.layer.masksToBounds = YES;
|
||||
[self setupUI];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - UI
|
||||
|
||||
// 构建视图层级与约束
|
||||
- (void)setupUI {
|
||||
[self addSubview:self.titleLabel];
|
||||
[self addSubview:self.descLabel];
|
||||
[self addSubview:self.appleContainer];
|
||||
[self addSubview:self.closeButton];
|
||||
|
||||
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self).offset(18);
|
||||
make.left.equalTo(self).offset(16);
|
||||
make.right.equalTo(self).offset(-16);
|
||||
}];
|
||||
[self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
|
||||
make.left.right.equalTo(self.titleLabel);
|
||||
}];
|
||||
[self.appleContainer mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self).offset(16);
|
||||
make.right.equalTo(self).offset(-16);
|
||||
make.top.equalTo(self.descLabel.mas_bottom).offset(20);
|
||||
make.height.mas_equalTo(52);
|
||||
make.bottom.equalTo(self).offset(-24); // 决定整体高度
|
||||
}];
|
||||
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self).offset(6);
|
||||
make.right.equalTo(self).offset(-6);
|
||||
make.width.height.mas_equalTo(36);
|
||||
}];
|
||||
|
||||
// 放置 Apple 登录按钮或占位
|
||||
if (@available(iOS 13.0, *)) {
|
||||
ASAuthorizationAppleIDButton *btn = [ASAuthorizationAppleIDButton new];
|
||||
[btn addTarget:self action:@selector(onTapApple) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self.appleContainer addSubview:btn];
|
||||
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self.appleContainer);
|
||||
}];
|
||||
} else {
|
||||
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
[btn setTitle:@"需要 iOS13+ 才能使用 Apple 登录" forState:UIControlStateNormal];
|
||||
btn.enabled = NO;
|
||||
btn.layer.cornerRadius = 8.0;
|
||||
btn.layer.borderWidth = 1.0;
|
||||
btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
|
||||
[self.appleContainer addSubview:btn];
|
||||
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self.appleContainer);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Actions
|
||||
|
||||
// 点击“用 Apple 登录”
|
||||
- (void)onTapApple {
|
||||
if (self.appleLoginHandler) self.appleLoginHandler();
|
||||
}
|
||||
|
||||
// 点击关闭按钮
|
||||
- (void)onTapClose {
|
||||
if (self.closeHandler) self.closeHandler();
|
||||
}
|
||||
|
||||
#pragma mark - Lazy
|
||||
|
||||
- (UILabel *)titleLabel {
|
||||
if (!_titleLabel) {
|
||||
_titleLabel = [UILabel new];
|
||||
_titleLabel.text = @"登录后可使用全部功能";
|
||||
_titleLabel.font = [UIFont boldSystemFontOfSize:18];
|
||||
_titleLabel.textColor = [UIColor blackColor];
|
||||
_titleLabel.textAlignment = NSTextAlignmentCenter;
|
||||
_titleLabel.numberOfLines = 1;
|
||||
}
|
||||
return _titleLabel;
|
||||
}
|
||||
|
||||
- (UILabel *)descLabel {
|
||||
if (!_descLabel) {
|
||||
_descLabel = [UILabel new];
|
||||
_descLabel.text = @"我们将使用 Apple 进行快速安全登录";
|
||||
_descLabel.font = [UIFont systemFontOfSize:14];
|
||||
_descLabel.textColor = [UIColor colorWithWhite:0.2 alpha:0.8];
|
||||
_descLabel.textAlignment = NSTextAlignmentCenter;
|
||||
_descLabel.numberOfLines = 0;
|
||||
}
|
||||
return _descLabel;
|
||||
}
|
||||
|
||||
- (UIButton *)closeButton {
|
||||
if (!_closeButton) {
|
||||
_closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
[_closeButton setTitle:@"✕" forState:UIControlStateNormal];
|
||||
[_closeButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
|
||||
_closeButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
|
||||
[_closeButton addTarget:self action:@selector(onTapClose) forControlEvents:UIControlEventTouchUpInside];
|
||||
}
|
||||
return _closeButton;
|
||||
}
|
||||
|
||||
- (UIView *)appleContainer {
|
||||
if (!_appleContainer) {
|
||||
_appleContainer = [UIView new];
|
||||
}
|
||||
return _appleContainer;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user