WatchKit openParentApplication: response

I am currently having a problem. My wish: if I open the WatchKit application, I will call "openParentApplication". I am receiving my data. But if I tested on real devices it doesn't work as I open the parent app in iPhone. But when I test in the simulator it works without opening the parent app.

My Xcode version is 6.3.2 and iOS 8.3.

What could be the problem?

InterfaceController.m

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    NSDictionary *userInfo = @{@"request":@"refreshData"};
    [WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error)
    {
        entries = replyInfo;
        NSLog(@"Reply: %@",replyInfo);
        [self reloadTable];
        [self.city setText:[entries valueForKey:@"city"][0] ];
    }];

}

      

AppDelegate.m

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
    NSString *refresh = [userInfo valueForKey:@"request"];
    if([refresh isEqualToString:@"refreshData"])
    {
        NSString *city = [[NSUserDefaults standardUserDefaults] stringForKey:@"City"];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:[NSString stringWithFormat:@"http://blackdriver.adappter.de/api/retrieve.php?city=%@",[city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             reply(responseObject);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             NSLog(@"Error: %@", error);
         }];
    }
}

      

EDIT - correct answer: See the link from mohammed alwaili in the comments

+3


source to share


2 answers


A openParentApplication:reply

request needs to be returned immediately, so you need to ask for extra time for the request asynchronous

to complete (run the request one at a time synchronous

, but that's a terrible practice).

From Tips & Tricks for Apple WatchKit Developers :



If your app on Apple Watch needs to run longer background tasks such as network calls, you must rely on your iPhone app to get the job done. Use the OpenParentApplication: reply: method in WKInterfaceController to wake up the iPhone app in the background and return the data your WatchKit extension needs. The UIApplicationDelegate method that handles the WatchKit request should return immediately. If an asynchronous call is required, for example for networking, use a background job to make sure your application is not blocked before it can send a response.

+3


source


I had a similar problem and had to solve several issues to get the WatchKit app to successfully invoke the iOS app that was making an asynchronous API call.

Here's a snippet of working code

func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
    let backgroundProcessingToken = application.beginBackgroundTaskWithName("backgroundApiCall", expirationHandler: { () -> Void in
        reply(["response":["error":"SOME_ERROR_CODE_INDICATING_TIMEOUT"]])
    })

    request(.GET, "https://api.forecast.io/forecast/[INSERT DARK SKY API CODE]/37.8267,-122.423").responseJSON(options: NSJSONReadingOptions.AllowFragments, completionHandler:{request, response, data, error in
        if(error != nil || data == nil){
            reply(["response":["error":"SOME_ERROR_CODE_INDICATING_FAILURE"]])
        }

        if let json = data as? NSDictionary {
            reply(["response":["data":json]])
        }

        application.endBackgroundTask(backgroundProcessingToken)
    })
}

      



Ultimately, you need to register as a background task to make sure your operating system doesn't get killed.

I also have a working example here on github FWIW

+2


source







All Articles