iOS16适配

作者: HF_K | 来源:发表于2022-09-15 15:10 被阅读0次

开启开发者模式

iOS升级后手机默认是未打开开发者模式的,这时候会出现如下问题:

  1. Xcode 14连接真机时,发现无法选择相应设备,提示信息是Developer Mode disabled,如下图。
  2. 已经安装过的测试App会出现无法运行的情况,提示信息如下图。
Developer Mode disabled 真机运行

解决办法:打开手机的调试模式,设置--隐私与安全性--开发者模式,打开开发者模式需要重启手机,同意即可。

PHImageManager.h报错

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.0.sdk/System/Library/Frameworks/Photos.framework/Headers/PHImageManager.h:18:2: error build: "Photos requires C++11 or later"

解决办法:

Go to the Build Settings-Apple Clang - Language - C++, then change

C++ Language Dialect
to
GNU++11 [-std=gnu++11]
worked for me.
Setting

Pod工程中的Bundle Target签名报错

方法一:自己设置Pod工程中的Bundle target 签名中的Team,选择与自己主工程一样即可。

方法二:Podfile文件中设置自己的开发者Team ID

post_install do |installer|
  installer.generated_projects.each do |project|
    project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings["DEVELOPMENT_TEAM"] = "Your Team ID"
         end
    end
  end
end

方法三:Podfile文件 中设置CODE_SIGN_IDENTITY

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '10.0'
            
            config.build_settings['EXPANDED_CODE_SIGN_IDENTITY'] = ""
            config.build_settings['CODE_SIGNING_REQUIRED'] = "NO"
            config.build_settings['CODE_SIGNING_ALLOWED'] = "NO"
        end
    end
end

post_install do |installer|
  installer.generated_projects.each do |project|
    project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['CODE_SIGN_IDENTITY'] = ''
         end
    end
  end
end

注意⚠️:推荐使用方法3 方法3两种都可,就是在Podfile脚本中设置CODE_SIGN_IDENTITY为空来避免报错,这是目前在用的,也是最简单的方法。

屏幕旋转

原方法

@try {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        UIDeviceOrientation val = UIDeviceOrientationLandscapeLeft;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
} @catch (NSException *exception) {
    
} @finally {
    
}

iOS 16UIKit上废弃了屏幕的横竖屏旋转方法。这一块之前有很多中处理方法,但是如果如上基于UIDevice的方法处理,那更新后就会遇到强制旋转屏幕不成功,日志如下,大体意思就是UIDevice.orientation已经不再被支持,我们之前进行的setOrientation其实是用KVC的形式去修改UIDevice里的readonly对象orientation

[Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)
日志

新增转屏

根据上方日志中Please use UIWindowScene.requestGeometryUpdate(_:)

方法

通过方法我们需要传入UIWindowSceneGeometryPreferences对象,是这次新增。

新方法

if (@available(iOS 16.0, *)) {
    [vc.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
    
    NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
    UIWindowScene *ws = (UIWindowScene *)array[0];
    UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
    geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskLandscapeLeft;
    [ws requestGeometryUpdateWithPreferences:geometryPreferences
        errorHandler:^(NSError * _Nonnull error) {
        //业务代码
    }];
}

注意⚠️:如果暂时不能用Xcode14,那么如下形式解决,原理和上面是一样的。

if (@available(iOS 16.0, *)) {
    @try {
        NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *ws = (UIWindowScene *)array[0];
        Class GeometryPreferences = NSClassFromString(@"UIWindowSceneGeometryPreferencesIOS");
        id geometryPreferences = [[GeometryPreferences alloc]init];
        [geometryPreferences setValue:@(UIInterfaceOrientationMaskLandscapeRight) forKey:@"interfaceOrientations"];
        SEL sel_method = NSSelectorFromString(@"requestGeometryUpdateWithPreferences:errorHandler:");
        void (^ErrorBlock)(NSError *err) = ^(NSError *err){
            //业务代码
        };
        if ([ws respondsToSelector:sel_method]) {
            (((void (*)(id, SEL,id,id))[ws methodForSelector:sel_method])(ws, sel_method,geometryPreferences,ErrorBlock));
        }
    } @catch (NSException *exception) {
        //异常处理
    } @finally {
        //异常处理
    }
}

ZFPlayer问题暂时修改

ZFPlayer切换横竖屏时也会出现问题,作者没有修复前,暂时修改。大佬已经更新,可以去更新三方了

找到ZFOrientationObserver中的- (void)interfaceOrientation:(UIInterfaceOrientation)orientation方法,进行修改如下

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
    // 虽然在iOS16设置里没用,但还是可以通过orientation获取到当前的旋转方向作为判断
       if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
           SEL selector = NSSelectorFromString(@"setOrientation:");
           NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
           [invocation setSelector:selector];
           [invocation setTarget:[UIDevice currentDevice]];
           UIInterfaceOrientation val = orientation;
           [invocation setArgument:&val atIndex:2];
           [invocation invoke];
       }
       if (@available(iOS 16.0, *)) {
           // 原来在shouldAutorotate里调用,iOS16改为手动调
           [UIWindow.zf_currentViewController setNeedsUpdateOfSupportedInterfaceOrientations];
           [self ls_shouldAutorotate];
           NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
           UIWindowScene *scene = (UIWindowScene *)array.firstObject;
           UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
           [geometryPreferences setValue:@(orientation) forKey:@"interfaceOrientations"];
           [scene requestGeometryUpdateWithPreferences:geometryPreferences
               errorHandler:^(NSError * _Nonnull error) {
               NSLog(@"--------Orientation Error: %@",error);
           }];
       }
}

注意⚠️:暂定如此修改,坐等大佬更新。大佬已经更新

相关文章

  • iOS16适配

    开启开发者模式 iOS升级后手机默认是未打开开发者模式的,这时候会出现如下问题: Xcode 14连接真机时,发现...

  • iOS16适配

    Q1: A:iOS16中,将无法再通过_statusBarWindow获取到状态栏window,注释或切换其他方式...

  • iOS16适配-屏幕旋转

    声明:本文适配以iOS 16 bate 2为基准 背景 iOS 16在UIKIT上有了一些更改,废弃掉了一些修改方...

  • iOS 项目无法在模拟器运行解决办法

    最近项目要适配iPhone14和iOS16,没有真机,只能在模拟器运行。之前项目都是在真机运行,没在模拟器运行过,...

  • iOS16适配指南之UICalendarView

    介绍 UICalendarView 是 iOS 16 中新增的视图,用于显示日历并支持同时选择日历中的一个或多个日...

  • iOS16适配指南之UINavigationItem

    增加了类型为 UINavigationItem.ItemStyle 的属性style用于描述 UINavigati...

  • iOS16适配指南之UIImage

    SF Symbols 中增加了新的类别 Variable,其中的图标支持可变渲染。 UIImage 相应地增加了新...

  • iOS16适配指南之UIMenu

    UIMenu增加了属性preferredElementSize用于设置菜单显示时内部元素的尺寸,共有 3 种尺寸可...

  • iOS16适配指南之UISheetPresentationCon

    在 iOS 15 中 Apple 推出了 UISheetPresentationController,通过它可以控...

  • XCode14 & iOS16 适配

    1.不升级电脑系统与 Xcode,调试iOS 16 1、下载iOS16 Support文件[https://git...

网友评论

    本文标题:iOS16适配

    本文链接:https://www.haomeiwen.com/subject/leorortx.html