Benjamin Herzog Benjamin Herzog - 11 months ago 69
iOS Question

How to wake up iPhone app from watchOS 2?

I have an app that has a very rich network layer and my apple watch app depends on all the models. Unfortunatly the app is not modular enough to make this layer available in the watch app.
I solved this problem by using openParentApplication: to wake up the iPhone app, perform the request and give back the results.
In watchOS 2 this method is gone and I should use WatchConnectivity. The best way to use this would be by sending userInfo dictionaries. But how can I wake up the iPhone app to handle my requests? To get notifications about new userInfos I have to use the WCSessionDelegate and for that I need a WCSession object. But when should I create that? And how to wake up the app? I hope you understand what I mean, english is not my native language.

Answer Source

I asked an Apple Engineer about this and got the following tip: The iOS-App should be started in a background-task. So the following worked for me pretty well:

UIApplication *application = [UIApplication sharedApplication];

__block UIBackgroundTaskIdentifier identifier = UIBackgroundTaskInvalid;
dispatch_block_t endBlock = ^ {
    if (identifier != UIBackgroundTaskInvalid) {
        [application endBackgroundTask:identifier];
    identifier = UIBackgroundTaskInvalid;

identifier = [application beginBackgroundTaskWithExpirationHandler:endBlock];

Add this to your session:didReceiveMessage: or session:didReceiveMessageData: method to start a background task with a three minute timeout.