I have been reading apple developer sources on how location updates work and reading tons stack overflow questions on it as well. Im trying to get my users location on my app all the time when the user put the app in the background. The code below is not my code, I found it from another stacker overflow source question and it is the only thing that gets me close to what i need. the code below works and updates me on the users location for 25 minutes only and then just stops, but what i want is it to work all the time as long as the app is in the background :(. The question was answered but I'm not quite understanding how this whole background stuff works. I've heard apple automatically terminates your app after 3 minutes or so, so anyway to bypass that would help. And also i do have my background modes checked as in Location updates and background fetch and even the info.plist that are needed as well which was said was suppose to automatically wake my app once terminated but that doesn't seem to work as well either. If anyone can help me this would be a tremendous help for my new app, it is the last bit of code i need aha, thank you in advance everyone. would also like to do it without adding a silent audio file so my app doesn't get rejected :)
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.allowsBackgroundLocationUpdates = true
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
if UIApplication.sharedApplication().applicationState == .Active {
} else {
backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
self.backgroundTimer.invalidate()
self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval( 60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
})
}
}
func updateLocation() {
var timeRemaining = UIApplication.sharedApplication().backgroundTimeRemaining
print(timeRemaining)
if timeRemaining > 60.0 {
print("timeRemaining > 60.0")
}
} else {
if timeRemaining == 0 {
print("timeRemaining = 0") UIApplication.sharedApplication().endBackgroundTask(backgroundTaskIdentifier)
}
backgroundTaskIdentifier2 = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
self.backgroundTimer.invalidate()
self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval( 60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
})
}
}