I have been working on an app to send the GPS position of the cell phone to a server through a TCP socket. The application must be able to remain in the background indefinitely. I have part of the echo code, the application sends the packet over TCP and the server receives it, it remains in the background but after a certain time the application stops sending data to the server. Apparently IOS closes it. If someone has any ideas or has done something similar, I would greatly appreciate your comments. This is the code I have:
class Location:NSObject, CLLocationManagerDelegate {
private var locationManager = CLLocationManager()
private var errorLocation: Bool = false
private var stuffLocation: CLLocation!
var outSocket: OutSocket!
var timer: NSTimer!
var stuff: Stuff!
override init() {
super.init()
outSocket = OutSocket()
setupLocation()
}
func setupLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.activityType = CLActivityType.Fitness
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if !errorLocation {
errorLocation = true
locationManager.allowDeferredLocationUpdatesUntilTraveled(10, timeout: 50)
stuffLocation = locations[0]
if timer == nil {
timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: #selector(Location.sendDataToServer), userInfo: nil, repeats: true)
}
}
}
}
func locationManager(manager: CLLocationManager, didFinishDeferredUpdatesWithError error: NSError?) {
errorLocation = false
}
The sendDataTiServer function is responsible for sending the data via TCP to the server. Any comment is welcomed. Greetings.