Send data Every 10 minutes

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    




// Override point for customization after application launch.

locmanager = [[CLLocationManager alloc] init]; 
[locmanager setDelegate:self]; 
[locmanager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//[locmanager setDistanceFilter:10];
updateTimer = [NSTimer timerWithTimeInterval:600 target:self selector:@selector(startUpdating) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSDefaultRunLoopMode];

[window makeKeyAndVisible];

return YES;
}

 -(void)startUpdating
{
[locmanager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
       if (newLocation.horizontalAccuracy < 0) return;


  CLLocationCoordinate2D loc = [newLocation coordinate];
 currentdate=[[NSDate date]timeIntervalSince1970];
   latitude = [NSString stringWithFormat: @"%f", loc.latitude];
 longitude= [NSString stringWithFormat: @"%f", loc.longitude];
//Call to webservice to send data
}

I want to send the coordinates to the web service every 10 minutes. Tried this, but it does not work. My application is registered to receive location updates in the background. Please suggest me the changes that need to be made for this program.

+3
source share
2 answers

I did something similar to this, using NSUserDefaults to record the last time sent last to the server, and using NSTimeInterval to compare between the updated date of the location data and this value. I use 30, but you can tune up. This is a bit of a hack, but it works with background launch, etc.

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

    updatedLocation = [newLocation retain];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSDate *myDate = (NSDate *)[prefs objectForKey:@"myDateKey"];

    NSDate *lastDate = (NSDate *)newLocation.timestamp;
    NSTimeInterval theDiff = [lastDate timeIntervalSinceDate:myDate];

    if (theDiff > 30.0f || myDate == nil){
             //do your webservices stuff here
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            [prefs setObject:lastDate forKey:@"myDateKey"];
            [prefs synchronize];

    }
+5
source

. didUpdateToLocation, , .

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
[manager stopUpdatingLocation];
etc...
0

Source: https://habr.com/ru/post/1773066/


All Articles