WatchKit在handleWatchKitExtensionRequest中的块内返回reply():

时间:2015-05-09 06:57:06

标签: ios objective-c watchkit apple-watch

我看到this SO帖子显然正在获取数据并返回到Watch扩展程序,如下所示:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply
{
  if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] )
 {
  // get data
  // ...
  reply( data );
 }
}

但是当我在获取网络数据之后尝试在块内调用'reply()'时:

__block UIBackgroundTaskIdentifier watchKitHandler;

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

NSMutableDictionary *response = [NSMutableDictionary dictionary];
[ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){

        if (succeeded)
        {
            [response setObject:@"update succeded" forKey:@"updateKey"];
            reply(response);

        }
        else
        {
            if (error)
            {
                [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; 
                reply(response);
            }
            else
            {
                [response setObject:@"update failed with no error" forKey:@"updateKey"];
                reply(response);
            }
        }
    }];
}

dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 180), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
});

我收到此错误:

  

“iPhone App中的UIApplicationDelegate从未调用reply() - [UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]”

所以我想我必须立即调用reply()并且在WatchKit唤醒父应用程序后发送新网络数据的唯一方法是使用MMWormhole吗?

2 个答案:

答案 0 :(得分:0)

我认为你的代码混乱了。你需要移动confirm进入你的var alert = 1, confirm = 2; document.body.innerHTML = alert + confirm; // 3使它成为你做的第一件事,然后调用你的功能。

简化示例:

.wrapper {
      display: table;

答案 1 :(得分:0)

始终参考官方文档。 Official Documentation

以下代码适用于我。

__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
                // Clean up any unfinished task business by marking where you
                // stopped or ending the task outright.
                [application endBackgroundTask:bgTask];
                bgTask = UIBackgroundTaskInvalid;
        }];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// Do the work associated with the task, preferably in chunks.

    [ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){

        if (succeeded)
        {
            [response setObject:@"update succeded" forKey:@"updateKey"];
            reply(response);

        }
        else
        {
            if (error)
            {
                [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; 

                reply(response);
            }
            else
            {
                [response setObject:@"update failed with no error" forKey:@"updateKey"];

                reply(response);
            }
        }
    }];
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});