i'm new user here, but I have used stackoverflow a lot of times :)... I'm android developer, but now i'm working in an ios app using swift. I'm totally new using xcode, maybe my question es very simple for a lot... Great, I really need help.
I'm trying to implement a backgroud task that updates the user location each ten minutes. I need to get the lat and long data and after send this info to an API web service. I have been rading the backgroud docs, all documentation, and all the documentation about user location services. There is a lot of information, pretty information, but, maybe i'm very wonted to the android information, with s lot of information too, but a lot of easy examples also, then, is very easy for me. and an previous stackoverflow questionmaybe helpme a lot.
I understand a lot of concepts, and I cofigured my info.plist like the documentation says, but... how can I run a background task for get the user location? Where I must write the background task code? What is the beginning? I have a project with three diferent viewcontrollers and I need to start the service with a button... Thank you so much, I really need to understand the start point of all this.
I am currently doing kind of the same thing as my first iOS-project - what helped me was this :
Periodic iOS background location updates
And from there on I found a Swift-Port of https://github.com/voyage11/Location: https://github.com/silento/Location. In the project from voyage11, there is also linked a blog post that a bit of clarifies what is happening and that also links to a second one that tells how to restart location services using "significant location changes". I am currently trying to implement this with "region location monitoring", but not sure if it will work.
Oh, and I moved the code that triggers a new background task out of the didUpdateLocation-part to the restartLocationUpdates-part - so you can be sure your task is restarted even if you do not get a location in time.
It did not work from the start, but when I added the code from https://stackoverflow.com/a/19085518/3085985 as a replacement for the stopUpdatingLocation() and startUpdatingLocation() - parts.
It is really more complicated than it should be and what I am used from Android - but it is how it is. If you are developing for Iphone5+ you can also consider this post (I could not test it right now as I only have an iPhone 4S and the feature is not supported in the Simulator): https://stackoverflow.com/a/24666487/3085985.
I hope you got some starting points, I am currently still testing my solution but it seems to work as far as I can see now ;)
P.S.: I somewhere read something about background tasks sleeping for more than 5 minutes being canceled - as I only wait 30 seconds I do not have this issue, but you might have. But maybe keeping location services running with a really high distance prevents this from happening.
P.P.S.: As Swift is new you should get used to reading Objective C-Code and trying to translate it to Swift. It often is the only way to find good tutorials.
One more thing - for iOS8 you need something like :
if (self.locationManager.respondsToSelector(Selector("requestWhenInUseAuthorization"))) {
// on iOS8+, we need to request the location authorization ourselves -.-
self.locationManager.requestAlwaysAuthorization()
}
And don't forget to add the "NSLocationAlwaysUsageDescription" to your Info.plist as well as allowing background location updates in your project capabilities.
Sorry for this mess of text, but it is a really complicated topic ;)
edit: Regarding your first comment :
You should create a Swift project, e.g. with a single window and in Main. storyboard add a switch. Then you should implement a ToggleActionChanged-event enabling or disabling the listeners, depending on the new state of the switch. I cannot explain all the basics, you should watch a basic tutorial, I like https://www.youtube.com/playlist?list=PL_4rJ_acBNMHa_RGZigG2nQI3aL1kTa4r and used especially the 7th video as an example of how to build a basic app. The only thing that can be made easier than shown there is using right-click-dragging from the storyboard to the code view instead of implementing some methods by hand. There is a handy assistant.
For your location event listeners you can add a new swift class and name it e.g. LocationDelegate.swift . It makes it a bit more clean. I use a Singleton-pattern for that, you can look up how to do this. Or you could use the structure of the github-project I posted above. I can't provide the complete source as you need to change it so it fits your individual needs ;)
Oh, and one more thing : To remember if your switch is enabled between application launches, you can use something like this :
@IBAction func toggleTrackingChanged(sender: UISwitch) {
// This is your method for when the switch changed... put your location activation/deactivation here
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setBool(sender.on, forKey: "ODLTrackingActive")
}
}
and in your viewDidLoad-Method :
let userDefaults = NSUserDefaults.standardUserDefaults()
var trackingActive = userDefaults.boolForKey("ODLTrackingActive")
toggleTrackingSwitch.setOn(trackingActive, animated: false)
// reactivate location tracking if necessary
Good luck!