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


    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    if ([[userInfo objectForKey:@"request"] isEqualToString:@"NearestFacility"]) {

        [self NearestFacilityClicked];
        if (self.ary_WatchKitNearestFacility.count>0) {
            NSDictionary *response = @{@"response" : self.ary_WatchKitNearestFacility};
            reply(response);
  ;      }
    }    

    dispatch_semaphore_signal(sema);
    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];
    });    
}

when trying to send NSMutableArray to Watch from iPhone app extension then it gives below error.

Error Domain=com.apple.watchkit.errors Code=2 "

The UIApplicationDelegate in the iPhone App never called reply() nsmutablearray

can anyone help me , how can I send NSMutableArray to watch from iPhone app, and this is dynamic array coming from REST request.

Thanks in advance

M. Porooshani
  • 1,797
  • 5
  • 34
  • 42
DevD
  • 1
  • 2
  • Follow this [Thread](http://stackoverflow.com/questions/27835694/the-uiapplicationdelegate-in-the-iphone-app-never-called-reply) To Achieve your goal. – C0mrade Oct 20 '15 at 09:43

1 Answers1

0

You need to use NSKeyedArchiver.

on iPhone:

NSData *objectData =
    [NSKeyedArchiver archivedDataWithRootObject:self.ary_WatchKitNearestFacility];
NSDictionary *response =
    [NSDictionary dictionaryWithObject:objectData forKey:@"response"];
reply(response);

on Watch:

NSData *objectData = replyInfo[@"response"];
NSMutableArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:objectData];

See also http://www.kristinathai.com/watchkit-best-practices-for-sharing-data-between-your-watch-and-ios-app/

EDIT

The thing is the reply() handler should be called in any case but in your example if [userInfo objectForKey:@"request"] is NOT NearestFacility OR self.ary_WatchKitNearestFacility.count == 0 the handler never won't be called.

Try the following code:

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


    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    NSDictionary *response = [[NSDictionary alloc] init];

    if ([[userInfo objectForKey:@"request"] isEqualToString:@"NearestFacility"]) {

        [self NearestFacilityClicked];
        if (self.ary_WatchKitNearestFacility.count>0) {
            NSDictionary *response = @{@"response" : self.ary_WatchKitNearestFacility};
        }
    }

    reply(response);

    dispatch_semaphore_signal(sema);
    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];
    });    
}

EDIT2

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
    // set a background task
    __block UIBackgroundTaskIdentifier backgroundTask;
    backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
        reply(nil);
    }];

    // do something
    if ([[userInfo objectForKey:@"request"] isEqualToString:@"NearestFacility"]) {

        [self NearestFacilityClicked];
        if (self.ary_WatchKitNearestFacility.count>0) {
            NSDictionary *response = @{@"response" : self.ary_WatchKitNearestFacility};
            reply(response);
        }
    }
    reply(nil);

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 2), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // end background task
        [application endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
    } );
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • Dear Kosuke, still i am getting same error after using NsKeyedArchiver and the error is : WatchKit Extension[569:71845] Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo={NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]} – DevD Oct 20 '15 at 09:32
  • Dear Kosuke, still it's not working in suspended state of application, i am running watch application and my iPhone app is in suspended state, if i put application in Active or in background state then its working fine for me, its sending the data from iPhone app to Watch app, but not working in suspended state :( – DevD Oct 20 '15 at 10:30
  • `dispatch_semaphore_t` could not be required. I edited my answer. ref. https://forums.developer.apple.com/message/73684#73684 – Kosuke Ogawa Oct 20 '15 at 13:07
  • Dear Kosuke, still i am getting same error :( DHADoctors WatchKit Extension[1112:320617] Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]" UserInfo={NSLocalizedDescription=The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]} – DevD Oct 21 '15 at 04:48
  • iOS 9.0.2 and watch OS 1.0.1 – DevD Oct 21 '15 at 04:49
  • @DevD Sorry I can't help you... You don't need to accept my answer. – Kosuke Ogawa Oct 21 '15 at 05:06
  • Thanks Kosuke for your help and prompt reply , thank you so much :) – DevD Oct 21 '15 at 05:46