My WatchKit extends the use of OpenParentApplication to upload data and images to the WatchKit application, as recommended by Apple. This works great, especially after using smart advice here:
http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply
However, it only works for about 15 minutes. After that, the WatchKit extension cannot wake the iPhone application in the background, and the iOS AppDelegate handleWatchKitExtensionRequest is never called. If I manually open the iPhone app, the call will go off and it will answer directly to the WatchKit extension, but this is hardly what I can ask the user ... I want the WatchKit app to be used without having to manually launch iOS every 15 minutes.
I am using iOS 8.2 on a test device Swift and Xcode 6.2. Maybe this could be due to the use of a developer training profile? Has anyone else experienced this?
The code used in the WatchUtils utility class is used in several cases in an application:
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 is used as follows:
WatchUtils.openPhoneApp(requestData, complete: { (reply, error) -> Void in
if let reply = reply {
}
})
AppDelegate:
func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {
var bogusTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
}
delay(2.0, closure: { () -> () in
UIApplication.sharedApplication().endBackgroundTask(bogusTask)
})
var replyDict:[NSObject : AnyObject] = ["userHighscore":45]
reply(replyDict)
UIApplication.sharedApplication().endBackgroundTask(bogusTask)
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(),
closure
)
}