-(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
以观看时,会出现以下错误。
错误Domain = com.apple.watchkit.errors Code = 2“
iPhone App中的UIApplicationDelegate
从未调用reply()
nsmutablearray
任何人都可以帮助我,如何从iPhone应用程序发送NSMutableArray
来观看,这是来自REST请求的动态数组。
提前致谢
答案 0 :(得分:0)
您需要使用NSKeyedArchiver
。
:
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 NearestFacility
或self.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];
});
}
<强> 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;
} );
}