背景
接入了新版的FirebaseSDK之后腾它,測(cè)試發(fā)現(xiàn)iOS12系統(tǒng)閃退了,而iOS13及以上版本沒問題死讹。
通過下載iOS12模擬器進(jìn)行測(cè)試瞒滴,發(fā)現(xiàn)iOS12模擬器啟動(dòng)崩潰了,里面的提示信息為
dyld: Library not loaded: /System/Library/Frameworks/SwiftUI.framework/SwiftUI
Referenced from: /Users/handsome/Library/Developer/CoreSimulator/Devices/B7DD7057-0DC7-47B0-B783-D2BA487CE81B/data/Containers/Bundle/Application/3C31E11E-3716-4176-9C0F-6B3521637D07/SDKDemo.app/SDKDemo
Reason: image not found
dyld: launch, loading dependent libraries
DYLD_FRAMEWORK_PATH=/Users/handsome/Library/Developer/Xcode/DerivedData/SDKDemo-cmgpdjdqjjgyinchqiukyuxwzynx/Build/Products/Debug-iphonesimulator
DYLD_FALLBACK_LIBRARY_PATH=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib
DYLD_ROOT_PATH=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot
DYLD_FALLBACK_FRAMEWORK_PATH=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks
DYLD_INSERT_LIBRARIES=/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 12.4.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrame
關(guān)鍵信息為 第一行
dyld: Library not loaded: /System/Library/Frameworks/SwiftUI.framework/SwiftUI
看樣子是SwiftUI的問題赞警,Google一波之后妓忍,確認(rèn)SwiftUI是iOS13開始使用,iOS13以下是沒有的愧旦。
如果使用了SwiftUI世剖,那么不進(jìn)行特別處理的話,在iOS12上會(huì)崩潰忘瓦,哪怕你使用了#available進(jìn)行處理搁廓。
因?yàn)?strong>使用了SwiftUI之后Xcode會(huì)默認(rèn)導(dǎo)入SwiftUI.framework庫。而且默認(rèn)導(dǎo)入的framework都是Required類型耕皮。
解決方案
知道了原因之后境蜕,解決方案也就出來了,就是將SwiftUI.framework系統(tǒng)庫改為可選凌停。目前有兩個(gè)解決方案:
方案一:
Build Phases選項(xiàng)卡中的 Link Binary With Libraries 里面添加 SwiftUI.framework粱年,然后將Status改為Optional。
方案二:
Build Settings 選項(xiàng)卡中的 Other Linker Flags 設(shè)置里面添加 -weak_framework SwiftUI
檢查配置
目前確認(rèn)是跟SwiftUI.framework相關(guān)罚拟,那么該怎么去檢測(cè)出的包究竟有沒有將SwiftUI.framework改為可選呢台诗?
我嘗試使用 otool -L 命令將IPA里面的可執(zhí)行文件的庫引入進(jìn)行比對(duì)完箩。
以下是會(huì)崩潰的IPA包分析結(jié)果(部分):
/System/Library/Frameworks/SwiftUI.framework/SwiftUI (compatibility version 0.0.0, current version 0.0.0)
.......(省略)
@rpath/libswiftUIKit.dylib (compatibility version 1.0.0, current version 5504.0.0, weak)
.......(省略)
以下是修改后沒問題的IPA包分析結(jié)果(部分):
/System/Library/Frameworks/SwiftUI.framework/SwiftUI (compatibility version 0.0.0, current version 0.0.0, weak)
.......(省略)
@rpath/libswiftUIKit.dylib (compatibility version 1.0.0, current version 5100.0.0, weak)
.......(省略)
/System/Library/Frameworks/SwiftUI.framework/SwiftUI (compatibility version 0.0.0, current version 0.0.0, weak)
很明顯,將SwiftUI.framework改為Optional后會(huì)多個(gè)weak的顯示拉队。
所以弊知,確實(shí)可以通過【otool -L 可執(zhí)行文件路徑】這個(gè)命令進(jìn)行檢查。SwiftUI.framework一行后面有weak代表已經(jīng)將其設(shè)置成Optional粱快,沒有那就是沒有設(shè)置秩彤。
參考文章
stackoverflow:dyld: Library not loaded SwiftUI when app runs on iOS 12 using @available(iOS 13.0, *)
https://github.com/firebase/firebase-ios-sdk/releases
https://firebase.google.com/support/release-notes/ios
蘋果開發(fā)者論壇:dyld: Library not loaded: /System/Library/Frameworks/SwiftUI.framework/SwiftUI
掘金:Xcode version 13.2 & 13.2.1 在iOS12上 由libswift_Concurrency.dylib 引起的crash修復(fù)