以下整理開發 Apple Watch 應用時實際遇到的八個問題,從除錯技巧、架構變遷到相容性處理都有。內容以 watchOS 1/2 時期為主,較新的 watchOS 版本在部分 API 上已有變動,閱讀時請留意時代背景。
Debug → Attach to Process by PID or name,填入 watch extension 的 bundle identifier。這樣中斷點就會生效。之所以要繞這一圈,是因為 Watch 程式的執行個體是由系統代為啟動的,Xcode 無法直接 attach。
做過 watchOS 1 開發的人應該都看過這張圖:

在 watchOS 1 上,Apple Watch 應用由兩部分構成:Watch App 與 WatchKit Extension。
也就是說,watchOS 1 時代所有「大腦」都在手機裡,手錶只是一塊遠端螢幕。這也解釋了為什麼早期 Watch App 的啟動速度那麼慢——每次操作都要把手機喚醒。
watchOS 2 的架構出現重大調整:

原本執行在 iPhone 上的 WatchKit Extension 被搬進 Apple Watch 裡。原本只存放資源與 storyboard 的 Watch App,現在連業務邏輯(程式碼執行部分)也一併放進去了。這個改動直接帶來更好的使用體驗:Watch App 的運行可以完全獨立於 iPhone。
watchOS 1 主要透過 WKInterfaceController 的這個方法:
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
reply:(nullable void (^)(NSDictionary *replyInfo, NSError *__nullable error))reply
WK_AVAILABLE_IOS_ONLY(8.2);
// 用於啟動手機上的宿主 iOS 應用,userInfo 不可為 nil把任務丟給 iPhone 處理。iPhone 收到後在 AppDelegate 中回呼:
- (void)application:(UIApplication *)application
handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply {
// 在這裡交由 NSURLSession 或 NSURLConnection 處理
}watchOS 2 因為架構調整,可以直接由手錶發起網路請求。值得注意的是 Apple Watch 支援 WiFi,因此在沒有 iPhone 在場的情況下也能取得網路資料。而手錶難以勝任的業務,則可以透過 WatchConnectivity 框架的 WCSession 請 iPhone 代為處理,再把結果回傳。
Apple Watch 的應用沒有真正意義上的後台,按表冠退出後原則上就不再運行(部分系統內建應用除外),而且下次啟動很快。但如果就是有強迫症想把程式徹底關掉,可以試試這個方法。以健身應用為例:
第一步:進入該應用的介面。

第二步:長按側邊按鈕,出現關機/省電模式介面後鬆開。

第三步:不要理會畫面內容,再次長按側邊按鈕,約三秒後應用會被完全終止。
接著再打開這個應用可能需要一點載入時間,這是正常的。
Watch App 是 Page-Based 的,可以左右滑動。假設有三個 interface controller,剛啟動時的呼叫順序大致如下:
-[InterfaceController init] -[InterfaceController awakeWithContext:] -[RepositoryInterfaceController init] -[RepositoryInterfaceController awakeWithContext:] -[SettingInterfaceController init] -[SettingInterfaceController awakeWithContext:] -[InterfaceController willActivate] -[InterfaceController didAppear] -[RepositoryInterfaceController willActivate] -[RepositoryInterfaceController didDeactivate]
可以注意到一個重要現象:三個 controller 的 init 與 awakeWithContext: 在啟動時就全部跑完了,但 willActivate/didAppear 只有目前可見的那一個被呼叫。這代表初始化時期的重活會拖慢整個頁面的啟動速度,適合放在 awake 的只有輕量設定。
從第一頁滑到第二頁之後:
-[InterfaceController willDisappear] -[RepositoryInterfaceController willActivate] -[InterfaceController didDeactivate] -[RepositoryInterfaceController didAppear] -[SettingInterfaceController willActivate] -[SettingInterfaceController didDeactivate]
順序上會發現:新頁面的 willActivate 早於舊頁面的 didDeactivate。這意味著你不能假設「切到新頁時舊頁一定已經完全停用」,如果兩頁共用某個資源,這裡容易出現競爭。
首先在 storyboard 中建立 Table 以及自訂的 row controller,row 對應一個 NSObject 子類別。
- (void)initTable {
[self.settingTable setNumberOfRows:2 withRowType:NSStringFromClass([SettingRow class])];
for (NSInteger i = 0; i < 2; i++) {
SettingRow *elementRow = (SettingRow *)[self.settingTable rowControllerAtIndex:i];
[elementRow.titleLabel setText:[NSString stringWithFormat:@"%@ 設置開發語言", @(i)]];
}
}點擊事件則需覆寫 WKInterfaceController 的方法:
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex {
if (rowIndex == 0) {
[self presentControllerWithName:NSStringFromClass([LanguageListInterfaceController class])
context:nil];
}
}Apple Watch 提供三種主要的互動入口:

設計上要注意的是 Glance 的定位:它是「一眼看完」的摘要,不是功能入口。把需要操作的功能塞進 Glance 是很常見的設計錯誤。
在 iOS 6 裝置上運行時會遇到這個錯誤:
dyld: Library not loaded: /System/Library/Frameworks/WatchKit.framework/WatchKit Referenced from: /var/mobile/Applications/xxxx/xxx.app/xxx Reason: image not found (lldb)
原因是 WatchKit.framework 在 iOS 6 上並不存在。解法是在 Build Phases 裡把 watchkit.framework 從 Required 改為 Optional。
這裡牽涉到 framework 的兩種連結方式:Optional(弱引用)與 Required(強引用)。強引用表示 App 啟動時就必須載入該 framework,找不到就會直接崩潰;弱引用則允許 framework 缺失,App 仍可啟動,只要在實際呼叫其 API 之前先用 respondsToSelector: 或版本判斷做保護即可。凡是為了相容舊系統而設定的 framework,都應該設為 Optional。
本文整理自 coderyi 的相關部落格文章。