我正在测试iPhone应用程序上某些代码的执行情况。我按照Apple建议的文档(不使用后台任务,只需控制台日志)。但是我在控制台上没有得到任何东西(我希望看到字符串“你好”)。
这是因为我在模拟器上运行WatchKit Extension应用程序?或者有什么我想念的东西?
如果您使用的是openParentApplication:reply:,请确保创建一个 进入后立即执行后台任务 应用:handleWatchKitExtensionRequest:回复:。这将确保 iPhone应用程序在后台获取时间而不是 再次暂停。另外,将调用包装到endBackgroundTask:in dispatch_after为2秒,以确保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)
答案 0 :(得分:3)
这是因为您只调试了WatchKit扩展目标。如果您想在控制台中看到您的主应用程序日志,则需要通过Xcode调试器附加主应用程序的进程。 转到调试 - &gt;附加进程 - &gt;(然后按标识符选择并键入应用程序名称)
为了更好地浏览,我专门为WatchKit / WatchKit扩展调试找到了这个很棒的资源: https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+is+currently+running+in+the+simulator
答案 1 :(得分:1)
答案 2 :(得分:0)
不知道是否偏离主题,但上面的代码中有一个简单的错误 在监视工具包中有initWithObjects:@ [requestString] forKeys:@ [@&#34; theRequestString&#34;]
并在appdelegate中NSString * request = [userInfo objectForKey:@&#34; requestString&#34;];
&#34; theRequestString&#34;不同意&#34; requestString&#34;
花了我一些时间来弄清楚为什么我的功能没有启动
感谢它帮助的代码