本文介绍了Apple Watch - 仅在手机上的应用程序处于活动状态时获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用模拟器开发并部署了我们的 Apple Watch 扩展应用程序.它已经获得批准,现在可以在应用商店上架,所以 Apple 很高兴!

I have developed and deployed our Apple Watch Extension App using the simulator. It's been approved and is available right now on the app store, so Apple are happy!

今天我能够拿到物理手表并发现一个真正的问题 - 我只能与手表手机交互,如果我手机上的应用程序不仅打开而且还处于活动状态......从而击败了对象.

Today I was able to get my hands on a physical watch and have discovered a real problem - I can only interact watch-phone, if the app on my phone is not just open, but also active...thus defeating the object.

我正在手机应用程序的 appDelegate 中实现以下方法作为代理来获取我的实时数据并将其返回给手表.

I am implementing the below method in the appDelegate of the phone app as a proxy to get my live data and return it to the watch.

我基本上将请求传递给 appDelegate - 我检查应返回数据的 userInfo,然后电话应用程序执行业务并返回包含手表要解析的数据的 userInfo 字典:

I basically pass in a request to the appDelegate - I check the userInfo for which data should be returned, then the phone app does the business and returns a userInfo dictionary containing data for the watch to parse:

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {

if ([userInfo objectForKey:@"GetWatchData"]) {

    [TPWatchData configureWatchData:^(BOOL success) {

        if (success) {

            reply([TPWatchData watchData]);

        } else {

            reply(@{@"FAIL":@"AppDelegate"});

        }


    }];

}

}

我在 watch extension 上使用这种方法在手表中调用它(只是我调用的一个示例):

I am calling it in the watch using this approach on the watch extension (just one example of the calls I make):

    [TPWPlanList openParentApplication:@{@"GetWatchData":@1} reply:^(NSDictionary *replyInfo, NSError *error) {

    if ([replyInfo objectForKey:@"Overview"]) {

        if (error.code == 0) {

            if ([replyInfo objectForKey:@"FAIL"]) {

                // Something failed

            } else {

                self.dic_overview = [replyInfo objectForKey:@"Overview"];

                //[self reloadPlanListTable];

            }

        } else {

            // Something failed

        }

    }

}];

如前所述,这在模拟器中运行良好,实际上在物理手表上运行良好 - 但前提是我在手机上打开了主应用程序并处于活动状态.应用程序在后台或手机显示屏休眠的那一刻,使用上述方法不会传递任何数据.

As already stated, this worked perfectly in the simulator and actually works perfectly on the physical watch - but ONLY if I have the main app open on the phone and active. The minute the app is in the background or the phone display sleeps, no data is passed using the above method.

有什么想法吗?我显然做错了什么,但我已经设法让整个事情在真实设备上直播,然后才被我咬到.

Any thoughts? I am clearly doing something wrong, but I've managed to get to the point of the entire thing being live and on real devices, before it's bitten me.

非常感谢!

推荐答案

如果您的 openParentApplication:reply: 方法仅在主机 iOS 应用程序处于活动状态时有效,我几乎可以保证您的 iOS 应用程序还没来得及回复就在后台被杀了.

If your openParentApplication:reply: method only works when the host iOS app is active, I can pretty much guarantee that your iOS app is being killed in the background before it has a chance to reply.

解决方案是让应用尽快响应请求,然后完成您的正常任务.开发者论坛中有关于此的说明:https://devforums.apple.com/thread/264473?tstart=0

The solution is to have the app respond to the request as quickly as possible, then complete your normal task. There's a note about this in the developer forums: https://devforums.apple.com/thread/264473?tstart=0

我还写了一篇博客文章,详细说明了在论坛中添加注释之前我使用的方法:http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply

I've also written a blog post detailing the methods I used before the note was added in the forums: http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply

这篇关于Apple Watch - 仅在手机上的应用程序处于活动状态时获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:22