iOS16适配

开启开发者模式

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适配)