This commit is contained in:
2025-11-12 21:23:31 +08:00
parent 0aead49816
commit bc261661ae
7 changed files with 317 additions and 24 deletions

View File

@@ -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 {

View 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

View 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