了解handleWatchKitExtensionRequest [英] Understanding handleWatchKitExtensionRequest

查看:127
本文介绍了了解handleWatchKitExtensionRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试iPhone应用程序上某些代码的执行情况.我遵循Apple建议的文档(不使用后台任务,只是一个控制台日志).但是我在控制台上什么都没得到(我想看到字符串"howdy").

I am testing the execution of some code on the iPhone app. I follow the documentation that Apple suggests (without using background tasks, just a console log). However I do not get anything on the console (I'd like to see the string "howdy").

这是因为我正在在模拟器上运行WatchKit Extension应用吗?还是我想念的东西?

Is this because I am running the WatchKit Extension app on the simulator? Or is there something that I am missing?

苹果说:

如果您使用的是openParentApplication:reply :,请确保您创建了一个 进入后立即执行后台任务 application:handleWatchKitExtensionRequest:reply:.这将确保 iPhone应用程序会在后台获取时间,而不是 再次暂停.此外,将对endBackgroundTask:的调用包装在 2秒的dispatch_after以确保iPhone应用程序有时间 在再次暂停之​​前发送回复.

If you are using openParentApplication:reply:, make sure you create a background task immediately upon entering application:handleWatchKitExtensionRequest:reply:. This will make sure that the iPhone app gets time in the background instead of being suspended again. Additionally, wrap the call to endBackgroundTask: in a dispatch_after of 2 seconds to ensure that the iPhone app has time to send the reply before being suspended again.

我在WatchKit扩展上的实现(链接到按钮的操作方法):

My implementation on the WatchKit extension (action method linked to a button):

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

我在控制台中得到的内容:

What I get in the console:

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)

推荐答案

这是因为您仅在调试WatchKit扩展目标.如果您还想在控制台中看到您的主应用程序日志,则需要通过Xcode调试器附加主应用程序的进程. 转到调试"->附加一个进程"->(然后按标识符选择并键入您的应用程序名称)

This is because you are only debugging the WatchKit extension target. If you wanted to see in the console your main application logs as well you will need to attach the main application's process through the Xcode debugger. Go to Debug-->Attach a process-->(Then select by identifier and type you applications name)

为了更好地进行遍历,我发现了一个非常有用的资源,专门用于WatchKit/WatchKit扩展调试: https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+正在+当前+在+ the + simulator中运行

For a better walk through I found this great resource for you specially for WatchKit/WatchKit extension debugging: https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+is+currently+running+in+the+simulator

这篇关于了解handleWatchKitExtensionRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆