Xcode 15 升级适配

2,520 阅读2分钟

1.升级Xcode版本

2.下载模拟器

Xcode 15 为简化安装包大小,除了 macOS 的 Components 外,不携带 iOS 17 模拟器,如果不安装模拟器,则无法真机和模拟器运行,更无法新建项目,所以要单独下载。

下载方式:

  1. 通过 Xcode 自身下载,但安装包过大且不支持断点续传,容易安装失败,要重新开始,不推荐

  2. 离线安装导入

    1. 通过开发者官网下载 iOS_17.2_Simulator_Runtime.dmg安装包

    2. 打开终端通过以下命令添加到 Xcode

      1. sudo xcode-select -s /Applications/Xcode.app(输入密码)
      2. xcodebuild -runFirstLaunch(等待一小会儿)
      3. xcrun simctl runtime add "/Users/xxxx/Downloads/iOS_17.2_Simulator_Runtime.dmg"
    3. 执行完毕后,到 Xcode 对应的 Settings - Platforms 界面查看是否安装成功

3.编译问题

Xcode 15 and iOS 17 - Error: DT_TOOLCHAIN_DIR cannot be used to evaluate LIBRARY_SEARCH_PATHS, use TOOLCHAIN_DIR instead

解决方式有以下两种:

1.升级 cocoapods 版本,官方已兼容修复该问题。 推荐使用

1. 升级Ruby环境
   1.1 查看当前版本
		gem -v
		ruby -v
   1.2 更新Ruby
		sudo gem update --system
2. 更新/安装 cocoapods 版本
	sudo gem install cocoapods
3. 回到工程下,执行更新操作
	pod update --verbose

2.直接修改 Podfile 文件,临时修复

# 临时修改 xcode 15 DT_TOOLCHAIN_DIR,等待 Cocoapods 官方修复
post_install do |installer|
  flutter_post_install(installer) if defined?(flutter_post_install)
​
  installer.aggregate_targets.each do |target|
    target.xcconfigs.each do |variant, xcconfig|
      xcconfig_path = target.client_root + target.xcconfig_relative_path(variant)
      IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
    end
  end
​
  installer.pod_target_subprojects.flat_map { |p| p.targets }.each do |target|
​
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
      config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
​
      if config.base_configuration_reference.is_a? Xcodeproj::Project::Object::PBXFileReference
        xcconfig_path = config.base_configuration_reference.real_path
        IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
      end
    end
  end
end

4.API适配

UIGraphicsBeginImageContextUIGraphicsBeginImageContextWithOptions 要校验传入的 size,要求 width > 0 并且 height > 0,否则会引发 crash

5.Xcode运行模拟器,控制台警告

Warning: Error creating LLDB target at path ''- using an empty LLDB target which can cause slow memory reads from remote devices.

解决方式:模拟器使用 Rosetta 模式运行。

  1. Xcode 14 之前:Xcode app 右键显示简介 -> 勾选 使用Rosetta打开

    Xcode14之前

  2. Xcode 14 之后:苹果在xcode的简介里没有了这个选项。Product-> Destination-> Destination Architecturesk 可以选择用哪种模式的模拟器打开。

    Xcode14之后

参考文档:www.jianshu.com/p/32917b7db…