[Catalyst]處理XCode11 將iOS App 打包成 Mac App 的警告問(wèn)題整理

Catalina.jpg

今年發(fā)布的bugOS13的同時(shí), 對(duì)蘋(píng)果開(kāi)發(fā)者們還有個(gè)好消息, 無(wú)論是Swift還是OC, 在XCode11下都可以打包為mac端應(yīng)用了.
筆者也試著跑了下自己的程序, 前前后后紅色警告起碼有20多個(gè), 整理了一遍, 如果有遺漏和錯(cuò)誤希望大家指正.

1. 更新所有舊API

老舊組件例如UIWebView等, 在Mac上已經(jīng)沒(méi)有了, 這也包括第三方庫(kù)中引用的, 需要統(tǒng)統(tǒng)替換掉. 如果你的項(xiàng)目是引用的CocoaPods的. 下次更新pod之后還需要再弄一次, 所以需要?jiǎng)冸x開(kāi)來(lái)(手動(dòng)引用這些第三方庫(kù),如AF等).

  1. 所有用到UIWebView的, 全部切換成 WKWebView
  2. 所有用到ALAssetsLibrary的, 全部切換 PhotoKit

2. 未簽名的庫(kù)

需要給代碼加上簽名


1_.pic.jpg

3. AFNetworking

  1. 第一點(diǎn)中說(shuō)的問(wèn)題UIWebView+AFNetworking. 刪~
  2. AFNetworking 3.2.1中調(diào)用了廢棄的方法
- (instancetype)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(nullable NSString *)path API_DEPRECATED_WITH_REPLACEMENT("initWithMemoryCapacity:diskCapacity:directoryURL:", macos(10.2,API_TO_BE_DEPRECATED), ios(2.0,API_TO_BE_DEPRECATED), watchos(2.0,API_TO_BE_DEPRECATED), tvos(9.0,API_TO_BE_DEPRECATED)) API_UNAVAILABLE(uikitformac);
=> API_UNAVAILABLE(uikitformac);

替換成

     return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
                                          diskCapacity:150 * 1024 * 1024
                                          directoryURL:[NSURL URLWithString:@"com.alamofire.imagedownloader"]] ;

4. 靜態(tài)庫(kù)中包含模擬器的包

因?yàn)?code>Mac端沒(méi)有iOS的模擬器

這幾個(gè)問(wèn)題解決之后. 那一抹亮眼的紅色終于消停了.
如果還有更多的適配希望大家能夠補(bǔ)充.


最后. 寫(xiě)兼容代碼

寫(xiě)兼容性代碼就是條件語(yǔ)句咯, XCode已經(jīng)為我們開(kāi)了路, 但還剩下許多細(xì)節(jié)需要實(shí)現(xiàn).
首先你要為項(xiàng)目增加新的target, 寫(xiě)入新的bundleID等, 增加target的細(xì)節(jié)可以參考這篇文章
在新的 target下進(jìn)入Building Settings, 找到PreprocessorMacros可以直接創(chuàng)建宏在代碼中使用.

  1. 為項(xiàng)目加入新的scheme或著自定義宏. 代碼中通過(guò)#ifdef, #ifndef宏, 進(jìn)行適配
#ifdef xxxScheme
...    
#else
...
#endif
  1. Conditional import
#if canImport(Crashlytics)
func dLog() { 
    // use Crashlytics symbols 
}
#endif
  1. CocoaPods

根據(jù)platform適配, 項(xiàng)目需要增加一個(gè)target專(zhuān)門(mén)對(duì)應(yīng)mac端,

target :testDemo do
platform:'ios','8.0'
pod 'AAA','~> 1.0'
end

target :testDemoMac do
platform:'osx','10.15'
pod 'BBB','~> 2.0'
end

題外話(huà): 根據(jù)scheme來(lái), 因?yàn)橛行┙M件做成模擬器和真機(jī)分開(kāi)的形式

  pod 'mySdk-release', :configurations => ['Release']
  pod 'mySdk', :configurations => ['Debug']

最終的Podfile文件是這個(gè)樣子


def commonPods
      pod 'a'
      # ...
end

target 'testDemo' do
  use_frameworks!
  platform:'ios','8.0'
  commonPods  
  pod 'b'
  pod 'c'
  # ...
end


target 'testDemoMac' do
  use_frameworks!
  platform:'osx','10.15'
  commonPods
  pod 'd'
  pod 'e'
  # ...
end


For More

module - Conditional Imports in Swift - Stack Overflow
https://guides.cocoapods.org/syntax/podfile.html
https://developer.apple.com/documentation/webkit/wkwebview
https://developer.apple.com/documentation/photokit
https://www.talentica.com/blogs/ios-build-management-using-custom-build-scheme/
https://guides.cocoapods.org/syntax/podfile.html#platform

最后編輯于
?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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