我正在 iPhone 应用程序上测试一些代码的执行。我遵循 Apple 建议的文档(不使用后台任务,仅使用控制台日志)。但是我在控制台上没有得到任何东西(我想看到字符串“howdy”)。
这是因为我在模拟器上运行 WatchKit 扩展应用程序吗?或者有什么我想念的吗?
苹果 说:
如果您使用 openParentApplication:reply:,请确保在输入 application:handleWatchKitExtensionRequest:reply: 后立即创建后台任务。这将确保 iPhone 应用程序在后台获得时间,而不是再次暂停。此外,在 2 秒的 dispatch_after 中包装对 endBackgroundTask: 的调用,以确保 iPhone 应用程序在再次暂停之前有时间发送回复。
我在 WatchKit 扩展上的实现(链接到按钮的操作方法):
- (IBAction)sendMessageToApp{
NSString *requestString = [NSString stringWithFormat:@"executeMethodA"]; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[requestString] forKeys:@[@"theRequestString"]];
[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
}];
NSLog(@"sending message..");
}
我在AppDelegate.m文件上的实现:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
NSString * request = [userInfo objectForKey:@"requestString"];
NSLog(@"howdy");
// This is just an example of what you could return. The one requirement is
// you do have to execute the reply block, even if it is just to 'reply(nil)'.
// All of the objects in the dictionary [must be serializable to a property list file][3].
// If necessary, you can covert other objects to NSData blobs first.
NSArray * objects = [[NSArray alloc] initWithObjects:[NSDate date],[NSDate date],[NSDate date], [NSDate date], nil];
NSArray * keys = [[NSArray alloc] initWithObjects:@"objectAName", @"objectdName", @"objectBName", @"objectCName", nil];
NSDictionary * replyContent = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
reply(replyContent);
}
我在控制台中得到的:
2015-04-16 14:43:44.034 FanLink WatchKit Extension[3324:142904] <InterfaceController: 0x608000081fe0> initWithContext
2015-04-16 14:44:04.848 FanLink WatchKit Extension[3324:142904] sennding message..
2015-04-16 14:44:04.854 FanLink WatchKit Extension[3324:142904]
Reply info: {
objectAName = "2015-04-16 13:44:04 +0000";
objectBName = "2015-04-16 13:44:04 +0000";
objectCName = "2015-04-16 13:44:04 +0000";
objectdName = "2015-04-16 13:44:04 +0000";
}
Error: (null)