Doing a webservice call to get the title of the notification when entering region

Hello,

When the device enters a region I want to do a webservice call to get the alert title of my local notification. But It seems it does not finish my method? You guys have any idea? Here is the code I use:

  • (void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
    {
    if([region.identifier isEqualToString:@"RegionIdentifier"]){
    NSString *path = [NSString stringWithFormat:@"/webservice/ibeacon/get-beacon-news/apikey/%@/id/%@/language/nl",@"apikey123",@"1"];
    NSURL *baseURL = [NSURL URLWithString:@"http://krc2014.preview.sanmax.be"];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];;

    [manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
     {
    
         // Success
         NSDictionary *json = (NSDictionary *)responseObject;
    
         NSLog(@"JSON %@",json);
         int status = [[json valueForKey:@"status"] intValue];
    
         if(status == 200){
             UILocalNotification *notification = [UILocalNotification new];
             notification.alertBody = [json valueForKeyPath:@"data.title"];
             [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
         }else{
    
         }
    
     }failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         // Failure
         NSLog(@"Failure: %@", error);
     }];
    

    }

}

Hi Stef,

My suspicion is, AFHTTPSessionManager makes an asynchronous request, and so before it manages to call the success/failure block, didEnterRegion already finishes execution and iOS puts the app back to sleep.

To ensure the app continues running till the HTTP response comes back, you can use the beginBackgroundTaskWithExpirationHandler: to start a background task. The app will then continue running in the background until you call endBackgroundTask (best to do it in the success/failure callback). There's an example code snippet in Apple's App Programming Guide for iOS, in the "Executing Finite-Length Tasks" section.