Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

iphone : using Background location services is stopped after a little time

i'm trying to get a location and compare it with a location online all that happen in background the method i create is working fine using background location service but after a minute or so the location icon in the status bar is getting disappear and the method is not getting called any longer

here is the code

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {


    double  lat = newLocation.coordinate.latitude;
    double  lon = newLocation.coordinate.longitude;
    NSURL * locationURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://somedomainname.com/iphoneLocation?lat=%f&lon=%f",lat,lon]];
    NSData * responseData = [NSData dataWithContentsOfURL:locationURL];
    NSString* aStr;
    aStr = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];



    if ([aStr isEqualToString:@"out Of Any Knowen Range"] ){
            UILocalNotification *notify =[[UILocalNotification alloc] init];
            notify.alertAction = [[NSString alloc] initWithString: @"View"];
            notify.fireDate=nil;
            notify.alertBody = [NSString stringWithFormat:@"New Data Occured"];
            notify.soundName = UILocalNotificationDefaultSoundName;
            NSLog(@"Local notification should display");
            [[UIApplication sharedApplication] presentLocalNotificationNow:notify];
        }

}

and in the viewDid load i'm using something like this

locationManager = [[CLLocationManager alloc] init];

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    locationManager.delegate = self;

    [locationManager startUpdatingLocation];

    CLLocation *userLocation = [locationManager location];

so what is the wrong with it

Upvotes: 2

Views: 642

Answers (2)

John Grant
John Grant

Reputation: 1679

I wonder if the Location Manager is getting released somewhere and therefore not sending any more updates.

Have you tried setting the location manager to a retained property in your view controller?

@property (nonatomic, strong) CLLocationManager *locationManager

Upvotes: 0

graver
graver

Reputation: 15213

You need to modify your AppName-Info.plist file by adding a key Required background modes with an item with value App registers for location updates. Another thing I think you should do as you connect online and this may not happen very fast, so the operation that you connect online, post the location and wait for response should start in another thread and meanwhile if you have received another location from CLLocationManager and your previous request has not finished yet to skip posting the new location...

Upvotes: 1

Related Questions