Requirement :My Watch app will show latest data from our server.
I tried :
To implement this thing I used
WKInterfaceController.openParentApplication(requestDict, reply: { (returnedObject, error) -> Void in
if (returnedObject != nil) {
//loading interface data here
}
})
In my app delegate function I used
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
// doing web service call using asynchronous nsurlconnection and replying with response dictionary
}
Problem : Problem is that application is running fine when iPhone application is foreground but watch app not showing anything when iPhone application is running in background. I debugged it and found actually when iPhone application is running background then webservice api call(nsurlconnection) is not retuuning any data, when it's coming to foreground then it's replying data to watch app.
To solve it I used nsuserdafults to store data, but problem is that it's not always showing latest data. Let consider user opened watch app and it will go to parent application and returning old data from userdafults.
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
if ([userInfo[@"type"] isEqualToString:@"list"]) {
[self fetchWatchData];//it will get and store data when app will be foreground
NSDictionary *replyDict = [UtiltiyManager getWatchData];//data from userdefaults
if (replyDict) {
reply(replyDict);
}else {
NSDictionary *noDataDict = @{@"data":@"nodata"};
reply(noDataDict);
}
}
}
Problem is that watch app can't get latest data from iphone while it's in background. As there are no service call api which will work in background. I checked with NSURLConnection and NSURLSessionDataTask both are foreground api call.
Any solutions or thoughts?
Update 1 :
Apple Docs Says :
Types of Tasks Within a session, the NSURLSession class supports three types of tasks: data tasks, download tasks, and upload tasks.
Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server. Data tasks can return data to your app one piece at a time after each piece of data is received, or all at once through a completion handler. Because data tasks do not store the data to a file, they are not supported in background sessions. Download tasks retrieve data in the form of a file, and support background downloads while the app is not running. Upload tasks send data (usually in the form of a file), and support background uploads while the app is not running.
Apple told data tasks is not available in background.And my data is small web service data that can be fetched using data task. so my service call is not download task. So in case of watch when iPhone app is background then how app will get web service data.
Should we use download task? But I guess it's intended to download any file.