Can we call WebService in locationManager didEnterRegion method when App is background? How to earn extra time to fulfill WS call?

I have set up one Geo Fence & monitoring using startMonitoringForRegion method. I am able to get events when Geo-fence region is entered & exited successfully. What i want to do is when i enter in certain geo-fence I want to make a Web Service call based on that location data & display Local Notification for Web Service Result [When App is in background / killed].

Will beginBackgroundTaskWithName method help?

In short:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
     // Make a Web Service Call to fetch data even 
     // **when App is killed/ in background mode.**
}
  1. As per my knowledge when geo fence is breached app comes into background mode. Not sure for how long.
  2. I can send Local Notification even app is killed/in background when geo fence is breached. Now what if i want to make web service call & display it's result as a notification when app is not in foreground.

What is the best approach to achieve this? Any help appreciated. Thanks in advance.

Answers


Found very useful stuff that every app has some extra time that iOS offers to resolve certain unfinished business before the app goes into suspended mode. For iOS 7 the time has been reduced to around 180 secs (3 mins); earlier it was around 600 secs (10 minutes).

To earn extra time we need to initialize UIBackgroundTaskIdentifier

-(void) beginBackgroundUploadTask
{
    if(self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [self endBackgroundUploadTask];
    }

    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        [self endBackgroundUploadTask];

    }];
}

Once the background task runs out of time from the stipulated allotted time, we should invalidate and end the background task.

-(void) endBackgroundUploadTask
{
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask ];
    self. backgroundTask = UIBackgroundTaskInvalid;
}

You can call the methods like this

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"Region Datas %@", region);

    NSUserDefaults *userid = [NSUserDefaults standardUserDefaults];
    NSString *get_userid=[userid stringForKey:@"USERID"];
    [userid synchronize];
    NSLog(@"User Id %@",get_userid);

    NSString* urlStr1 =[NSString stringWithFormat:@"http://www.test.net/test.php"];
    NSLog(@"Enter Url %@",urlStr1);
    NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr1]];
    [NSURLConnection sendAsynchronousRequest:request1
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {
         if (data.length > 0 && connectionError == nil)
         {
             NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
             NSLog(@"Success Result %@",result);
         }
         else
         {
             NSLog(@"Error Result %@",connectionError);
         }
     }];

}

Need Your Help

How to get the values dynamically added controls in other parts of the code?

c# asp.net visual-studio-2010

I have a method that creates textboxes depending on the number of parameters values that the user has to enter.And its working fine. The problem is that when the user clicks Ok i want to take the v...

Not in In SQL statement?

sql sql-server tsql set-difference notin

I have set of ids in excel around 5000 and in the table I have ids around 30000. If I use 'In' condition in SQL statment I am getting around 4300 ids from what ever I have ids in Excel. But If I us...