繁体   English   中英

错误Domain = com.apple.watchkit.errors代码= 2“ iPhone应用程序中的UIApplicationDelegate从未调用Reply()nsmutablearray

[英]Error Domain=com.apple.watchkit.errors Code=2 "The UIApplicationDelegate in the iPhone App never called reply() nsmutablearray

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

尝试从iPhone应用程序扩展名发送NSMutableArray到Watch时,它给出以下错误。

错误Domain = com.apple.watchkit.errors代码= 2“

iPhone应用程序中的UIApplicationDelegate从未调用reply() nsmutablearray

谁能帮助我,如何从iPhone应用程序发送NSMutableArray进行观看,这是来自REST请求的动态数组。

提前致谢

您需要使用NSKeyedArchiver

在iPhone上:

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

在手表上:

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

另请参见http://www.kristinathai.com/watchkit-best-practices-for-sharing-data-between-your-watch-and-ios-app/

编辑

问题是在任何情况下都应调用Reply()处理函数,但在您的示例中,如果[userInfo objectForKey:@"request"] is NOT NearestFacilityself.ary_WatchKitNearestFacility.count == 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);

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

编辑2

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM