1

I have read a few questions/answers before asking, and also attached process, but does not seem to work.

My watchkit extension is written in Swift, and the AppDelegate is in Objective-C (since it is old code).

In my extension, I called:

@IBAction func toPage2() {
    println("to page 2")
    WKInterfaceController.openParentApplication(["page":"2"], reply: {(reply, error) -> Void in })
}

In my AppDelegate, I tried printing

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSLog(@"handle watchkit");

NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"page2"] ofType:@"wav"];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:nil];
[audio play];
...

I have also tried Debug > Attach to process and choose the iPhone app, and still nothing happened. Can anyone points me to a direction?

Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88
  • Does this problem occur also in the simulator when the main iPhone app is active? Maybe this helps: http://stackoverflow.com/questions/30000274/calling-parent-application-from-watch-app – John May 06 '15 at 14:58

2 Answers2

1

Start a background task in handleWatchKitExtensionRequest as specified in the documentation. This ensures that the main app on the iPhone is not suspended (when the app on the iPhone is not active) before it can send its reply.

Code in the app delegate of the main app on iPhone:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
   __block UIBackgroundTaskIdentifier watchKitHandler;
   watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                               expirationHandler:^{
                                                                 watchKitHandler = UIBackgroundTaskInvalid;
                                                               }];

   if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
   {
      // get data
      // ...
      reply( data );
   }

   dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1 ), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
       [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
    } );
}
Pang
  • 9,564
  • 146
  • 81
  • 122
John
  • 8,468
  • 5
  • 36
  • 61
0

You will need to register for background mode Audio and then take your code and wrap it in a UIBackgroundTask Also you need to return a reply() as part of your handleWatchKitExtensionRequest

Here is a good link on background tasks

rmp
  • 3,503
  • 1
  • 17
  • 26
  • 1
    You do not need to register for a background mode. This will cause your app to be potentially rejected if you aren't playing background audio. – bgilham May 06 '15 at 16:53
  • You are using `handleWatchKitExtensionRequest` which will launch your app into the background if it is not already running in the foreground. – rmp May 06 '15 at 16:57
  • I am running the app in foreground... nothing prints out, nothing plays. – Lim Thye Chean May 06 '15 at 23:46