Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...