Reputation: 101
The scenario is.
1) I have already existing iOS app for iPhone device. The application is showing the real time data in dashboard page. The data on Dashboard is updated after every 60 sec by calling the web services from iOS app.
2) I want to develop the apple watch application based on same iPhone app Which will show the dashboard with data updating after every 60 seconds.
How to achieve this. Any suggestions are highly appreciated. Thanks.
Upvotes: 0
Views: 815
Reputation: 2741
I had the same scenario to perform. So what i did in my App is:
In your didFinishLaunchingWithOptions
method in AppDelegate.m
timer = [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(refreshData) userInfo:nil repeats:YES];
refreshData
method looks like
-(void)refreshData
{
// Update Database Calls
// below line Save new time when update request goes to server every time.
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"databaseUpdateTimestamp"]; //
}
Now Add a timer in your willActivate
method in watchKit
timer = [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(refreshData) userInfo:nil repeats:YES];
the refreshData
method will call request to parent App every 2 min.
- (void) refreshData
{
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"database",@"update", nil];
[WKInterfaceController openParentApplication:dic reply:^(NSDictionary *replyInfo, NSError *error)
{
NSLog(@"%@ %@",replyInfo, error);
}];
}
Now in your App Delegate in Parent App
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
if([userInfo objectForKey:@"update"])
{
NSString *strChceckDB = [userInfo objectForKey:@"update"];
if ([strChceckDB isEqualToString:@"database"])
{
NSDate *dateNow = [NSDate date];
NSDate *previousUpdatedTime = (NSDate*)[[NSUserDefaults standardUserDefaults] objectForKey:@"databaseUpdateTimestamp"];
NSTimeInterval distanceBetweenDates = [dateNow timeIntervalSinceDate:previousUpdatedTime];
if (distanceBetweenDates>120) // this is to check that no data request is in progress
{
[self.timer invalidate]; // your orignal timer in iPhone App
self.timer = nil;
[self refreshData]; //Method to Get New Records from Server
[self addTimer]; // Add it again for normal update calls
}
}
}
else
{
//do something else
}
}
Use This Updated Data to Populate your apps dashboard in WatchKit App.
Some Useful Links That will help you to complete this:
You can create an embedded framework to share code between your app extension and its containing app.
Hope this help you ....!!!
Upvotes: 1