Apple Watch - only getting data if app on phone is active

二次信任 提交于 2019-11-26 23:05:55

问题


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.

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.

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

        }


    }];

}

}

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.

Many thanks!


回答1:


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.

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

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



来源:https://stackoverflow.com/questions/29848353/apple-watch-only-getting-data-if-app-on-phone-is-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!