我的WatchKit扩展程序大量使用openParentApplication将数据和图像加载到WatchKit应用程序中,正如Apple所建议的那样。这工作正常,特别是在使用这里的聪明建议后: http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply
然而,它只能工作约15分钟。之后,WatchKit扩展无法在后台唤醒iPhone应用程序,并且永远不会调用iOS AppDelegate handleWatchKitExtensionRequest。如果我手动打开iPhone应用程序,呼叫就会通过并直接回答WatchKit扩展,但这不是我可以要求用户做的事情...我希望WatchKit应用程序可用而无需手动唤醒iOS应用每15分钟一次。
我在测试设备上使用iOS 8.2,Swift和Xcode 6.2。也许它可能与使用开发人员配置文件有关?有没有其他人经历过这个?
实用程序类WatchUtils中使用的代码,用于应用程序中的多个部分:
class func openPhoneApp(userInfo: [NSObject : AnyObject], complete:(reply:[NSObject : AnyObject]!, error:NSError!) -> Void) {
WKInterfaceController.openParentApplication(userInfo, reply: { (reply:[NSObject : AnyObject]!, error:NSError!) -> Void in
complete(reply:reply, error:error)
})
}
openPhoneApp的使用方式如下:
WatchUtils.openPhoneApp(requestData, complete: { (reply, error) -> Void in
if let reply = reply {
// Do stuff
}
})
的AppDelegate:
func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {
// Bogus task for keeping app going
var bogusTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
}
// End bogus task after 2 seconds
delay(2.0, closure: { () -> () in
UIApplication.sharedApplication().endBackgroundTask(bogusTask)
})
// Code here for collecting information from phone app
// ...
var replyDict:[NSObject : AnyObject] = ["userHighscore":45]
reply(replyDict)
UIApplication.sharedApplication().endBackgroundTask(bogusTask) // End the bogusTask in case the work was faster than 2 seconds
}
// Utility function for delay
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(),
closure
)
}
答案 0 :(得分:0)
我有同样的问题并且阅读文档为我修复了它。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleWatchKitExtensionRequest:reply:
因为在您的应用程序在后台时可能会调用此方法,所以在执行开始时调用beginBackgroundTaskWithName:expirationHandler:方法,并在处理完回复并执行回复块后调用endBackgroundTask:方法。启动后台任务可确保您的应用在有机会发送回复之前不会被暂停。
我的一些代码:
nint taskId = 0;
public override void HandleWatchKitExtensionRequest (UIApplication application, NSDictionary userInfo, Action<NSDictionary> reply)
{
try {
taskId= UIApplication.SharedApplication.BeginBackgroundTask(delegate
{ //this is the action that will run when the task expires
});
if (userInfo != null && userInfo.Keys [0] != null && userInfo.Keys [0].ToString() == "match") {
MyWebClient wc = new MyWebClient ();
wc.Encoding = Encoding.UTF8;
wc.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) => {
String res = e.Result;
reply (new NSDictionary (
"justTesting", new NSString (res)
));
if(taskId != 0)
UIApplication.SharedApplication.EndBackgroundTask(taskId);
};
wc.DownloadStringAsync(new Uri(myUrl));
}
}
在我改变这样做之后,它已经稳定了几天,而以前它总是在5-15分钟后停止工作。