今年發(fā)布的bugOS13
的同時(shí), 對蘋果開發(fā)者們還有個(gè)好消息, 無論是Swift
還是OC
, 在XCode
11下都可以打包為mac端應(yīng)用
了.
筆者也試著跑了下自己的程序, 前前后后紅色警告
起碼有20多個(gè), 整理了一遍, 如果有遺漏和錯(cuò)誤希望大家指正.
1. 更新所有舊API
老舊組件例如UIWebView
等, 在Mac上已經(jīng)沒有了, 這也包括第三方庫中引用的, 需要統(tǒng)統(tǒng)替換掉. 如果你的項(xiàng)目是引用的CocoaPods
的. 下次更新pod
之后還需要再弄一次, 所以需要?jiǎng)冸x開來(手動(dòng)引用這些第三方庫,如AF等).
- 所有用到
UIWebView
的, 全部切換成WKWebView
- 所有用到
ALAssetsLibrary
的, 全部切換PhotoKit
2. 未簽名的庫
需要給代碼加上簽名
3. AFNetworking
- 第一點(diǎn)中說的問題
UIWebView+AFNetworking
. 刪~ -
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)庫中包含模擬器的包
因?yàn)?code>Mac端沒有iOS的模擬器
這幾個(gè)問題解決之后. 那一抹亮眼的紅色
終于消停了.
如果還有更多的適配希望大家能夠補(bǔ)充.
最后. 寫兼容代碼
寫兼容性代碼就是條件語句咯, XCode
已經(jīng)為我們開了路, 但還剩下許多細(xì)節(jié)需要實(shí)現(xiàn).
首先你要為項(xiàng)目增加新的target
, 寫入新的bundleID
等, 增加target
的細(xì)節(jié)可以參考這篇文章
在新的 target
下進(jìn)入Building Settings
, 找到PreprocessorMacros
可以直接創(chuàng)建宏在代碼中使用.
- 為項(xiàng)目加入新的
scheme
或著自定義宏
. 代碼中通過#ifdef
,#ifndef
宏, 進(jìn)行適配
#ifdef xxxScheme
...
#else
...
#endif
- Conditional
import
#if canImport(Crashlytics)
func dLog() {
// use Crashlytics symbols
}
#endif
CocoaPods
根據(jù)platform
適配, 項(xiàng)目需要增加一個(gè)target
專門對應(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
題外話: 根據(jù)scheme
來, 因?yàn)橛行┙M件做成模擬器和真機(jī)分開的形式
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