背景#
測試說 iOS 12 的手機上安裝 Xcode14.0.2 導出的新包後,打開就崩潰,但是在系統版本高的手機上就沒有問題。
調試後發現,崩潰日誌是dyld: Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib
,具體如下:
dyld: Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib
Referenced from: /private/var/containers/Bundle/Application/55730273-D9D6-4C42-9335-7A56F92B7F2C/xxx.app/Frameworks/FSPagerView.framework/FSPagerView
Reason: image not founds
搜索後發現,開發者社區中有此問題的記錄,xcode14 not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib,解決方案是:
If you are building your app with Xcode 14 or Xcode 14.0.1, your app will crash on older OS versions, including while building and testing directly from Xcode, as well as after submitting the app to the App Store. The OS versions affected include iOS 11 - 12.1, macOS 10.13 - 10.14.3, as well as watchOS and tvOS versions from the same time frame. To work around the crash when building with Xcode 14, add -Wl,-weak-lswiftCoreGraphics (substituting other library names for swiftCoreGraphics if appropriate for your crash) to the Other Linker Flags build setting.
即添加-Wl,-weak-lswiftCoreGraphics
到Build Settings
中的Other Linker Flags
中。
這裡要注意是添加到Other Linker Flags
而不是Other Swift Flags
,如果遇到下面的報錯,就說明你和我一樣,添加到了錯誤的地方。。。。
Driver threw unknown argument: '-weak-libswiftCoreGraphics' without emitting errors.
注意 1#
然而需要注意的是,假如項目有多個Target
,如果添加在Target
中,就要針對每個Target
都要添加一次,很是麻煩,所以可以直接在PROJECT
下的Build Settings
中添加。
注意 2#
在項目中添加了-Wl,-weak-lswiftCoreGraphics
到Other Linker Flags
之後,編譯運行發現還是會崩潰,還是報錯
dyld: Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib
Referenced from: /private/var/containers/Bundle/Application/55730273-D9D6-4C42-9335-7A56F92B7F2C/xxx.app/Frameworks/FSPagerView.framework/FSPagerView
Reason: image not founds
仔細看了之後,發現報錯中提示的FSPagerView
,是CocoaPods
三方庫,所以找到對應的第三方庫,然後在對應庫的Build Settings
中找到Other Linker Flags
,然後添加-Wl,-weak-lswiftCoreGraphics
,再運行,發現還是報錯,但是換了另一個三方庫。。。
針對每個三方庫一個個添加,是不可能的,太麻煩不說,每次Pod install
之後就需要重新再設置,不是正確的解決辦法。
所以有沒有可能,在Podfile
中post_install
添加設置,統一一次性給所有三方庫加這個編譯設置。當然可以,設置如下:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['OTHER_LDFLAGS'] = '-Wl,-weak-lswiftCoreGraphics'
end
end
end
然後編譯,發現報錯,因為項目中有些庫沒有用到swiftCoreGraphics
,比如 OC 的三方庫,或者非 UI 的庫,所以還是要改,需要區分添加。針對項目中 Swift 類型的 UI 相關的庫,添加這個編譯選項,其他的不添加,最終示例如下:
need_otherlinkerflags_frameworks = ['FSPagerView', 'HandyJSON', 'IQKeyboardManagerSwift', 'JXSegmentedView', 'KDCircularProgress', 'Kingfisher', 'RxSwift', 'PKHUD', 'RxCocoa', 'SnapKit', 'ZLPhotoBrowser']
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if need_otherlinkerflags_frameworks.include?(target.name)
config.build_settings['OTHER_LDFLAGS'] = '-Wl,-weak-lswiftCoreGraphics'
end
end
end
end
Pod install
後,編譯運行,發現可以正常運行了。再驗證一下,非低版本手機上是否受到影響,沒有影響,完美。
Done!