• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 了解 handleWatchKitExtensionRequest

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:41:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在测试一些代码在 iPhone 应用上的执行。我遵循 Apple 建议的文档(不使用后台任务,仅使用 控制台日志)。但是我在控制台上没有得到任何东西(我想看到字符串“howdy”)。

这是因为我在模拟器上运行 WatchKit 扩展应用吗?还是我遗漏了什么?


苹果说:

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 扩展上的实现(链接到按钮的操作方法):

- (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)applicationUIApplication *)application handleWatchKitExtensionRequestNSDictionary *)userInfo replyvoid(^)(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] initWithObjectsbjects 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)



Best Answer-推荐答案


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

为了更好地浏览,我为您找到了这个很好的资源,专门用于 WatchKit/WatchKit 扩展调试: https://mkswap.net/m/blog/How+to+debug+an+iOS+app+while+the+WatchKit+app+is+currently+running+in+the+simulator

关于ios - 了解 handleWatchKitExtensionRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29677132/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap