问题
I am implementing a kind of tracking app and i need to spot my location once and send it once per 10 - 20 seconds (period value is important and can't be exceeded). To lower battery consumption i stop location updates. This works good in foreground, but how can i do it when app moved in background? I looked info about background fetch, but it hasn't got precise time for periodic sending data
How can i perform this task?
回答1:
You can start and Stop periodic location update while app is in background. To achieve this add class from given link for Location Update.
After that import LocationTracker.h in your AppDelegate.
Add Below code in your didFinishLaunchingWithOptions.
let locationTracker : LocationTracker = LocationTracker();
locationTracker?.startLocationTracking();
In LocationTracker.m, you can set duration to restart update.Here i set 1 minute or 60 Seconds.
//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
selector:@selector(restartLocationUpdates)
userInfo:nil
repeats:NO];
You can also set duration time for fetch Locations. Here I fetch location for 10 Seconds.
self.shareModel.delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];
来源:https://stackoverflow.com/questions/34512007/periodic-sending-small-data-to-server-in-background-mode