iOS16 适配问题 剪贴板问题

iOS16 适配问题 剪贴板问题

1.隐私权限增强,如通过 UIDevice 获取设备名称时,无法获取用户的信息,只能获取设备对应的名称。(经验证iOS16任仍可使用获取设备类型,名称,系统版本)

2.UIDevice 不再支持通过setValue()方法设置设备的方向,替换为UIWindowScenerequestGeometryUpdate()方法。

3.iOS 16 更新了UIPasteControl

影响点:每次调用UIPasteboard获取用户剪切板,都会弹出授权弹窗告知用户.目前无法阻止弹窗弹出,新的API UIPasteControl 需要用户主动点击.

 - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (@available(iOS 16.0, *)) {

        UIPasteControlConfiguration *config = [[UIPasteControlConfiguration alloc]init];
        config.baseBackgroundColor = [UIColor orangeColor];
        config.baseForegroundColor = [UIColor greenColor];
        config.cornerStyle = UIButtonConfigurationCornerStyleCapsule;
        config.displayMode = UIPasteControlDisplayModeIconAndLabel;
        _pc = [[UIPasteControl alloc] initWithConfiguration:config];
        _pc.target = self.targetTextField;

        _pc.frame = CGRectMake(0, 0, 200, 60);
        _pc.center = self.view.center;
        NSLog(@"粘贴板%@",_pc.target);
        _pc.target = self.view;
        [self.view addSubview:_pc];
        [self.view addSubview:self.targetTextField];
    } else {
        NSString *copy = [[UIPasteboard generalPasteboard] string];
        NSLog(@"粘贴板%@",copy);
    }
    
    
}
- (UITextField *)targetTextField {
    
    if (!_targetTextField) {
        
        _targetTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 30)];
        
        _targetTextField.borderStyle = UITextBorderStyleRoundedRect;
        
        [_targetTextField addTarget:self
                             action:@selector(targetTextFieldAction:)
                   forControlEvents:UIControlEventEditingChanged];
        [_targetTextField becomeFirstResponder];
    }
    
    return _targetTextField;
}

- (void)targetTextFieldAction:(UITextField *)textField {
    
    NSLog(@"粘贴值%@", textField.text);
    NSLog(@"粘贴板%@,控制器%@",_pc.target, _pc.configuration);
}
原有的UIPasteboard还是可用的,在iOS16会有个提示框,用户同意即可读取用户剪贴板(16.0.2 同开发账号下的应用&应用内主动粘贴不在弹窗)
新的UIPasteControl属性如下:
UIKIT_EXTERN API_AVAILABLE(ios(16.0)) API_UNAVAILABLE(tvos, watchos) NS_SWIFT_UI_ACTOR
@interface UIPasteControl : UIControl
@property (nonatomic, readonly) UIPasteControlConfiguration *configuration;
@property (nonatomic, nullable, weak) id target;

- (instancetype)initWithConfiguration:(UIPasteControlConfiguration  *)configuration NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder     NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFrame:(CGRect)frame     NS_DESIGNATED_INITIALIZER;

and

UIKIT_EXTERN API_AVAILABLE(ios(16.0))       API_UNAVAILABLE(tvos, watchos) NS_SWIFT_NAME(UIPasteControl.Configuration) NS_SWIFT_UI_ACTOR
@interface UIPasteControlConfiguration : NSObject 
@property (nonatomic, assign) UIPasteControlDisplayMode displayMode;
@property (nonatomic, assign) UIButtonConfigurationCornerStyle cornerStyle NS_REFINED_FOR_SWIFT;
@property (nonatomic, assign) CGFloat cornerRadius;
@property (nonatomic, nullable, strong) UIColor     *baseForegroundColor;
@property (nonatomic, nullable, strong) UIColor     *baseBackgroundColor;
@end

and

UIKIT_EXTERN API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos) NS_SWIFT_UI_ACTOR
@protocol UIPasteConfigurationSupporting 

@property (nonatomic, copy, nullable) UIPasteConfiguration *pasteConfiguration;

@optional
- (void)pasteItemProviders:(NSArray *)itemProviders;
- (BOOL)canPasteItemProviders:(NSArray *)itemProviders;

@end
适配方法: 使用UIPasteControl 需要升级Xcode14 (Xcode14兼容的最低版本为iOS11)
截图1.png
升级iOS16 测试版描述文件:https://d-updater.i4.cn/web/mobileconfig/iOS_16_DP_Beta_Profile.mobileconfig
截图2.png
使用UIPasteboard 每次使用粘贴板会弹窗告知用户,用户授权同意后才可使用
截图3.png

注:UIScreen.main即将被废弃,建议使用(UIApplication.shared.connectedScenes.first as? UIWindowScene)?.screen

10月25更新

今天更新了iOS16.1,应用在弹过授权粘贴框后,可以在设置里面找到对应的app,点击<从其他App粘贴>,选择允许后,不再弹 粘贴授权弹窗.
WechatIMG255.jpeg

WechatIMG254.jpeg

你可能感兴趣的:(iOS16 适配问题 剪贴板问题)