iOS模擬器適配2023-03-17

接手的公司iOS項(xiàng)目是組件化開(kāi)發(fā),項(xiàng)目也比較大,App Store顯示的包有400多M,并且無(wú)法適配模擬器。所以,首先我來(lái)進(jìn)行模擬器適配。

1、報(bào)錯(cuò)building for iOS Simulator, but linking in object file built for iOS

如報(bào)錯(cuò)

In /Users/zhanyu/Workspace/iOS/********_iOS/Pods/ZYMapKit/BaiduMapFramework/BaiduMapAPI_Utils.framework/BaiduMapAPI_Utils(BMKOpenPoiOption.o), building for iOS Simulator, but linking in object file built for iOS, file 
'/Users/zhanyu/Workspace/iOS/********_iOS/Pods/ZYMapKit/BaiduMapFramework/BaiduMapAPI_Utils.framework/BaiduMapAPI_Utils' for architecture arm64

這個(gè)問(wèn)題目前應(yīng)該只出現(xiàn)在M1芯片的Mac上,似乎又是一個(gè)兼容性問(wèn)題,因?yàn)镸1芯片的Mac本身就是arm架構(gòu),所以模擬器自然也支持arm架

第一步修改Podfile文件
#        config.build_settings['VALID_ARCHS'] = ['arm64', 'x86_64','arm64-apple-ios']
        config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
第二步修改,在編譯設(shè)置中,對(duì)模擬器排除 arm64
205eeeed9a71efcbc2541e357ae3cc0f.png

然后pod install
再檢查

第三步:在主項(xiàng)目和pod項(xiàng)目的PROJECT的Build Settings中,刪除VALID_ARCHS(整個(gè)刪除,不能只刪除value留下Key
jietudjhfjds.png

Xcode 12不再支持VALID_ARCHS,需移除!采用Excluded Architectures過(guò)濾掉不需要的架構(gòu)即可!
如果項(xiàng)目在Build Settings下的User-Defined中添加了VALID_ARCHS屬性,需要將其移除,否則無(wú)法在Xcode 12編譯運(yùn)行設(shè)備。
如果確實(shí)需要設(shè)定支持/不支持的架構(gòu),通過(guò)Excluded Architectures配置即可。

ps:ios模擬器運(yùn)行報(bào)錯(cuò)
Command PhaseScriptExecution failed with a nonzero exit code,也可以嘗試以上方式解決

2、模擬器不支持的庫(kù)瑞修德、推送、PKI、PLPlaykit、愛(ài)心推PLMediaStreamingKit

如報(bào)錯(cuò)Could not find module 'RXD_SDK' for target 'x86_64-apple-ios-simulator'; found: arm64, arm64-apple-ios, at:

先用如以下方式過(guò)濾import和調(diào)用代碼
#if targetEnvironment(simulator)
import RXD_SDK
#else
#endif

如報(bào)錯(cuò)
Undefined symbol: OBJC_CLASS$_HDtuiliuVideoViewController
在HDtuiliuVideoViewController類(lèi)中找到代碼引用PLPlaykit代碼

#if TARGET_IPHONE_SIMULATOR
#else
                // 初始化 PLPlayerOption 對(duì)象
                PLPlayerOption *option = [PLPlayerOption defaultOption];
                // 更改需要修改的 option 屬性鍵所對(duì)應(yīng)的值
                [option setOptionValue:@10 forKey:PLPlayerOptionKeyTimeoutIntervalForMediaPackets];
                [option setOptionValue:@2000 forKey:PLPlayerOptionKeyMaxL1BufferDuration];
                [option setOptionValue:@1000 forKey:PLPlayerOptionKeyMaxL2BufferDuration];
                [option setOptionValue:@(YES) forKey:PLPlayerOptionKeyVideoToolbox];
                [option setOptionValue:@(kPLLogNone) forKey:PLPlayerOptionKeyLogLevel];
                self.player = [PLPlayer playerLiveWithURL:[NSURL URLWithString:self.model.playUrl] option:option];
                self.player.delegate = self;
                self.player.playerView.frame = CGRectMake(0, 180, kScreenWidth, 250);
                [self.videoView addSubview:self.player.playerView];
                [self.player play];
#endif

如調(diào)用了PLMediaStreamingKit中代碼也報(bào)錯(cuò),可以修改

#if TARGET_IPHONE_SIMULATOR
#else
    [PLStreamingEnv initEnv];
#endif

其他的地方,如

#if targetEnvironment(simulator)
#else
        if viewController is QYSessionViewController {
            printLog(message: "已進(jìn)入客服頁(yè)面")
#endif

#if targetEnvironment(simulator)
#else
        let option = QYSDKOption.init()
        option.appKey = ThirdLibConfig.shared.QY_NIM_Appkey
        option.appName = ""
        option.isFusion = false
        QYSDK.shared().register(with: option)
#endif



    /** 獲取Wifi Ssid */
     public func getSsid() -> String? {
        if let interface : CFArray = CNCopySupportedInterfaces() {
#if targetEnvironment(simulator)
#else
            NSLog("\(interface)");
            for i in 0..<CFArrayGetCount(interface) {
                let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interface, i);
                NSLog("\(interfaceName)");
                let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
                if let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString), let interfaceData = unsafeInterfaceData as? [String : AnyObject] {
                    if let ssid =  interfaceData["SSID"] as? String {
                        return ssid
                    }
                }
            }
        }
#endif
        return nil;
    }

3、七牛 - Qiniu (= 7.2.5)版本中HappyDNS問(wèn)題

dyld[52532]: Library not loaded: @rpath/HappyDNS.framework/HappyDNS
 Reason: tried: '/Users/zhanyu/Library/Developer/Xcode/DerivedData/***-fjmngiqwdvbcdyasnbmgeutgqouy/Build/Products/Debug-iphonesimulator/HappyDNS.framework/HappyDNS' (errno=2), '/Users/zhanyu/Library/Developer/CoreSimulator/Devices/CFD4C541-F60A-4190-8CEB-4CFC402A7431/data/Containers/Bundle/Application/3C0A8B63-C41B-49CA-8073-960CF74711ED/*****.app/Frameworks/HappyDNS.framework/HappyDNS' (fat file, but missing compatible architecture (have 'armv7,arm64', need 'x86_64')),

PLShortVideoKit在podspec中dependency了Qiniu7.2.5,Qiniu7.2.5有個(gè)0.3版本HappyDNS.framework依賴,但是這個(gè)版本壓根沒(méi)用還會(huì)導(dǎo)致報(bào)錯(cuò)。
這個(gè)問(wèn)題我想了很多方式解決都沒(méi)用,
如果升級(jí)PLShortVideoKit到3.2.5,七牛變成8.2.0,會(huì)導(dǎo)致其他報(bào)錯(cuò)。
如果刪除HappyDNS.framework的引用 -framework "HappyDNS",模擬器能跑起來(lái),但是pod install后,刪除的內(nèi)容又會(huì)復(fù)原。

最終方法:

在本地引入PLShortVideoKit.podspec,版本還是3.1.1,但不依賴s.dependency 'Qiniu', '7.2.5'
在本地引入Qiniu.podspec,版本還是7.2.5,但不依賴s.dependency 'HappyDNS', '~> 0.3'
然后再Podfile中

  #  PLShortVideoKit用自己編寫(xiě)的podspec,剔除HappyDNS,解決HappyDNS不適配模擬器問(wèn)題
  pod 'PLShortVideoKit', :podspec => 'PLShortVideoKit.podspec'
  pod 'Qiniu', :podspec => 'Qiniu.podspec'

pod install后完美解決問(wèn)題
模擬器能跑起來(lái)了

打包時(shí)爆了個(gè)錯(cuò)

error: Build input file cannot be found: '/Users/zhanyu/Library/Developer/Xcode/DerivedData/***-bilorlkikiiqdgbzqzutxjdoxkfz/Build/Intermediates.noindex/ArchiveIntermediates/JFDriver/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/JFDriverWidgetExtension.appex/***WidgetExtension'. Did you forget to declare this file as an output of a script phase or custom build rule which produces it? (in target '***WidgetExtension' from project 'JFDriver')

在***WidgetExtension target 以下地方release也要進(jìn)行設(shè)置


截屏2023-03-17 17.48.34.png

如果一個(gè).framework只有真機(jī)庫(kù),需要在模擬器中運(yùn)行,此時(shí)加載庫(kù)的時(shí)候會(huì)報(bào)錯(cuò)。
解決方法:在Excluded Source File Names中配置
只有在模擬器環(huán)境下,不使用的庫(kù):(如果在代碼中還要加上#if TARGET_OS_SIMULATOR進(jìn)行模擬器的邏輯判斷。)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容