3
This commit is contained in:
19
CustomKeyboard/View/KBFullAccessGuideView.h
Normal file
19
CustomKeyboard/View/KBFullAccessGuideView.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// KBFullAccessGuideView.h
|
||||
// CustomKeyboard
|
||||
//
|
||||
// A lightweight overlay prompting user to enable "Allow Full Access".
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface KBFullAccessGuideView : UIView
|
||||
|
||||
/// Present the guide overlay inside a given parent view (or its window).
|
||||
+ (void)showInView:(UIView *)parent;
|
||||
|
||||
/// Dismiss if shown.
|
||||
+ (void)dismissFromView:(UIView *)parent;
|
||||
|
||||
@end
|
||||
|
||||
185
CustomKeyboard/View/KBFullAccessGuideView.m
Normal file
185
CustomKeyboard/View/KBFullAccessGuideView.m
Normal file
@@ -0,0 +1,185 @@
|
||||
//
|
||||
// KBFullAccessGuideView.m
|
||||
// CustomKeyboard
|
||||
//
|
||||
|
||||
#import "KBFullAccessGuideView.h"
|
||||
#import "Masonry.h"
|
||||
|
||||
@interface KBFullAccessGuideView ()
|
||||
@property (nonatomic, strong) UIControl *backdrop;
|
||||
@property (nonatomic, strong) UIView *card;
|
||||
@end
|
||||
|
||||
@implementation KBFullAccessGuideView
|
||||
|
||||
+ (instancetype)build {
|
||||
KBFullAccessGuideView *v = [[KBFullAccessGuideView alloc] initWithFrame:CGRectZero];
|
||||
[v setupUI];
|
||||
return v;
|
||||
}
|
||||
|
||||
- (void)setupUI {
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
|
||||
self.backdrop = [[UIControl alloc] init];
|
||||
self.backdrop.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.35];
|
||||
// [self.backdrop addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self addSubview:self.backdrop];
|
||||
[self.backdrop mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.edges.equalTo(self);
|
||||
}];
|
||||
|
||||
self.card = [[UIView alloc] init];
|
||||
self.card.backgroundColor = [UIColor whiteColor];
|
||||
self.card.layer.cornerRadius = 16;
|
||||
self.card.layer.masksToBounds = YES;
|
||||
[self addSubview:self.card];
|
||||
[self.card mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.center.equalTo(self);
|
||||
make.left.equalTo(self).offset(28);
|
||||
make.right.equalTo(self).offset(-28);
|
||||
}];
|
||||
|
||||
UILabel *title = [UILabel new];
|
||||
title.text = @"开启【允许完全访问】,体验完整功能";
|
||||
title.font = [UIFont boldSystemFontOfSize:16];
|
||||
title.textColor = [UIColor blackColor];
|
||||
title.textAlignment = NSTextAlignmentCenter;
|
||||
[self.card addSubview:title];
|
||||
[title mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.card).offset(16);
|
||||
make.left.right.equalTo(self.card).insets(UIEdgeInsetsMake(0, 16, 0, 16));
|
||||
}];
|
||||
|
||||
// 模拟两行开关(纯展示,不真实控制)
|
||||
UIView *box = [UIView new];
|
||||
box.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1.0];
|
||||
box.layer.cornerRadius = 12;
|
||||
[self.card addSubview:box];
|
||||
[box mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(title.mas_bottom).offset(12);
|
||||
make.left.equalTo(self.card).offset(16);
|
||||
make.right.equalTo(self.card).offset(-16);
|
||||
make.height.mas_equalTo(100);
|
||||
}];
|
||||
|
||||
UILabel *row1 = [UILabel new]; row1.text = @"恋爱键盘"; row1.textColor = [UIColor blackColor];
|
||||
UILabel *row2 = [UILabel new]; row2.text = @"允许完全访问"; row2.textColor = [UIColor blackColor];
|
||||
[box addSubview:row1]; [box addSubview:row2];
|
||||
[row1 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(box).offset(16); make.top.equalTo(box).offset(14); }];
|
||||
UIView *line = [UIView new]; line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
|
||||
[box addSubview:line];
|
||||
[line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(box).offset(12); make.right.equalTo(box).offset(-12); make.centerY.equalTo(box); make.height.mas_equalTo(1); }];
|
||||
[row2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(box).offset(16); make.bottom.equalTo(box).offset(-14); }];
|
||||
|
||||
// 右侧绿色开关的装饰
|
||||
UIView* (^switchView)(void) = ^UIView *{
|
||||
UIView *sw = [UIView new];
|
||||
sw.backgroundColor = [UIColor colorWithRed:0.12 green:0.75 blue:0.35 alpha:1.0];
|
||||
sw.layer.cornerRadius = 15;
|
||||
UIView *dot = [UIView new];
|
||||
dot.backgroundColor = [UIColor whiteColor];
|
||||
dot.layer.cornerRadius = 12;
|
||||
[sw addSubview:dot];
|
||||
[dot mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(sw); make.right.equalTo(sw).offset(-3); make.width.height.mas_equalTo(24); }];
|
||||
[sw mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(52); make.height.mas_equalTo(30); }];
|
||||
return sw;
|
||||
};
|
||||
UIView *sw1 = switchView(); UIView *sw2 = switchView();
|
||||
[box addSubview:sw1]; [box addSubview:sw2];
|
||||
[sw1 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(box).offset(-12); make.centerY.equalTo(row1); }];
|
||||
[sw2 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(box).offset(-12); make.centerY.equalTo(row2); }];
|
||||
|
||||
UIButton *go = [UIButton buttonWithType:UIButtonTypeSystem];
|
||||
go.backgroundColor = [UIColor blackColor];
|
||||
[go setTitle:@"去开启" forState:UIControlStateNormal];
|
||||
[go setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
||||
go.titleLabel.font = [UIFont boldSystemFontOfSize:18];
|
||||
go.layer.cornerRadius = 12;
|
||||
[go addTarget:self action:@selector(onTapGoEnable) forControlEvents:UIControlEventTouchUpInside];
|
||||
[self.card addSubview:go];
|
||||
[go mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(box.mas_bottom).offset(16);
|
||||
make.left.equalTo(self.card).offset(16);
|
||||
make.right.equalTo(self.card).offset(-16);
|
||||
make.height.mas_equalTo(48);
|
||||
make.bottom.equalTo(self.card).offset(-16);
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)presentIn:(UIView *)parent {
|
||||
UIView *container = parent.window ?: parent;
|
||||
self.frame = container.bounds;
|
||||
self.alpha = 0;
|
||||
[container addSubview:self];
|
||||
[self mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(container); }];
|
||||
[UIView animateWithDuration:0.2 animations:^{ self.alpha = 1; }];
|
||||
}
|
||||
|
||||
- (void)dismiss {
|
||||
[UIView animateWithDuration:0.18 animations:^{ self.alpha = 0; } completion:^(BOOL finished) {
|
||||
[self removeFromSuperview];
|
||||
}];
|
||||
}
|
||||
|
||||
+ (void)showInView:(UIView *)parent {
|
||||
if (!parent) return;
|
||||
// 避免重复
|
||||
for (UIView *v in (parent.window ?: parent).subviews) {
|
||||
if ([v isKindOfClass:[KBFullAccessGuideView class]]) return;
|
||||
}
|
||||
[[KBFullAccessGuideView build] presentIn:parent];
|
||||
}
|
||||
|
||||
+ (void)dismissFromView:(UIView *)parent {
|
||||
UIView *container = parent.window ?: parent;
|
||||
for (UIView *v in container.subviews) {
|
||||
if ([v isKindOfClass:[KBFullAccessGuideView class]]) {
|
||||
[(KBFullAccessGuideView *)v dismiss];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#pragma mark - Actions
|
||||
|
||||
- (UIInputViewController *)kb_findInputController {
|
||||
UIResponder *res = self;
|
||||
while (res) {
|
||||
if ([res isKindOfClass:[UIInputViewController class]]) {
|
||||
return (UIInputViewController *)res;
|
||||
}
|
||||
res = res.nextResponder;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)onTapGoEnable {
|
||||
// 在扩展中无法使用 UIApplication。改为委托宿主打开链接:
|
||||
// 方案:优先拉起主 App 并由主 App 打开设置页,避免宿主拦截。
|
||||
UIInputViewController *ivc = [self kb_findInputController];
|
||||
if (!ivc) { [self dismiss]; return; }
|
||||
|
||||
// 先尝试 Universal Link(如未配置可改为你的域名),失败再用自定义 scheme。
|
||||
NSURL *ul = [NSURL URLWithString:@"https://your.domain/ul/settings?src=kb_extension"];
|
||||
void (^fallback)(void) = ^{
|
||||
NSURL *scheme = [NSURL URLWithString:@"kbkeyboard://settings?src=kb_extension"]; // 主App在 openURL 中处理
|
||||
[ivc.extensionContext openURL:scheme completionHandler:^(__unused BOOL ok2) {
|
||||
// 无论成功与否,都收起当前提示层,避免遮挡
|
||||
[self dismiss];
|
||||
}];
|
||||
};
|
||||
|
||||
if (ul) {
|
||||
[ivc.extensionContext openURL:ul completionHandler:^(BOOL ok) {
|
||||
if (ok) { [self dismiss]; }
|
||||
else { fallback(); }
|
||||
}];
|
||||
} else {
|
||||
fallback();
|
||||
}
|
||||
}
|
||||
@end
|
||||
@@ -11,8 +11,11 @@
|
||||
#import "KBFunctionTagCell.h"
|
||||
#import "Masonry.h"
|
||||
#import <MBProgressHUD.h>
|
||||
#import "KBFullAccessGuideView.h"
|
||||
|
||||
static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
|
||||
// 通用链接(替换为你的真实域名 + 路径,并在主App配置 Associated Domains 与 AASA)
|
||||
static NSString * const kKBUniversalLinkLogin = @"https://your.domain/ul/login";
|
||||
|
||||
@interface KBFunctionView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, KBFunctionBarViewDelegate>
|
||||
// UI
|
||||
@@ -161,21 +164,28 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId";
|
||||
return 12.0;
|
||||
}
|
||||
|
||||
// 用户点击功能标签:拉起主 App,并由主 App 侧弹出登录浮层
|
||||
// 用户点击功能标签:优先 UL 拉起主App,失败再 Scheme;两次都失败则提示开启完全访问
|
||||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
UIInputViewController *ivc = [self findInputViewController];
|
||||
if (!ivc) return;
|
||||
|
||||
NSString *title = (indexPath.item < self.itemsInternal.count) ? self.itemsInternal[indexPath.item] : @"";
|
||||
NSString *encodedTitle = [title stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] ?: @"";
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"kbkeyboard://login?src=functionView&index=%ld&title=%@", (long)indexPath.item, encodedTitle]];
|
||||
if (!url) return;
|
||||
|
||||
NSURL *ul = [NSURL URLWithString:[NSString stringWithFormat:@"%@?src=functionView&index=%ld&title=%@", kKBUniversalLinkLogin, (long)indexPath.item, encodedTitle]];
|
||||
if (!ul) return;
|
||||
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[ivc.extensionContext openURL:url completionHandler:^(BOOL success) {
|
||||
if (!success) {
|
||||
NSLog(@"[KB] openURL failed. Likely Full Access is OFF. 请到 设置>通用>键盘>键盘>你的键盘 开启“允许完全访问”。");
|
||||
}
|
||||
[ivc.extensionContext openURL:ul completionHandler:^(BOOL ok) {
|
||||
if (ok) return; // Universal Link 成功
|
||||
|
||||
NSURL *scheme = [NSURL URLWithString:[NSString stringWithFormat:@"kbkeyboard://login?src=functionView&index=%ld&title=%@", (long)indexPath.item, encodedTitle]];
|
||||
[ivc.extensionContext openURL:scheme completionHandler:^(BOOL ok2) {
|
||||
if (!ok2) {
|
||||
// 两条路都失败:大概率未开完全访问或宿主拦截。给出指引层。
|
||||
dispatch_async(dispatch_get_main_queue(), ^{ [KBFullAccessGuideView showInView:self]; });
|
||||
}
|
||||
}];
|
||||
}];
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user