我正在開發一個Apple Watch應用程序,該應用程序使用openParentApplication:reply:
方法與其父應用程序進行通信。我的WatchKit應用程序中openParentApplication的行爲不一致
父應用程序與Web服務進行通信,並通過調用包含數據的NSDictionary
調用reply
方法將其獲得的數據發回給手錶擴展。
當父應用程序在前臺或後臺打開時,該應用程序可以很好地工作。但是,如果我打開父應用程序,然後使用任務切換器終止它,則手錶分機第一次撥打電話時,它會收到以下錯誤,並且參數replyInfo
以零形式出現。
UIApplicationDelegate in the iPhone App never called reply()
但每一個電話分機,使該得到恰當的反應之後。
我檢查並發現,手錶擴展名首次撥打電話時,handleWatchKitExtensionRequest:reply:
永遠不會在父應用上被調用。
這可能是什麼原因?
我按照文檔中的建議在後臺任務中執行handleWatchKitExtensionRequest:reply:
中的所有操作。下面是我的一些代碼: 代碼從我的延伸:從父應用
NSDictionary *params = @{@"requestCode": @(RequestGetLoggedIn)};
[WKInterfaceController openParentApplication:params reply:^(NSDictionary *replyInfo, NSError *error) {
// Do something with the result
}];
代碼:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
}];
NSNumber* requestCode = userInfo[@"requestCode"];
// Perform some request and then call reply()
// End the background task
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
});
}
編輯1:無論在模擬器和真正的Apple關注出現的問題。
也許[我的答案在這裏](http://stackoverflow.com/questions/29750310/watchkit-handlewatchkitextensionrequest-multiple-instances/29765448#29765448)可以幫助你 – zisoft
我不認爲這是這種情況,因爲一切正常在第一次通話後正確。如果'application:didFinishLaunchingWithOptions:'有問題,那麼應用程序永遠不會在後臺打開,因此調用永遠不會得到任何響應。 –
爲了響應編輯#2:爲什麼在方法幫助結束時啓動後臺任務?您需要啓動後臺任務_immediately_並在其中執行您的工作。我不認爲這是iOS中的錯誤。這聽起來像你的應用程序正在被殺死(由於缺乏背景任務),纔有機會回覆。 – bgilham