{"_id":"5c6b54f9f0fee50222702a46","project":"55e67aaa9cc7c62b00c4a1ea","version":{"_id":"5b720760c44b7600034b7a08","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"forked_from":"5b1f2cbdfd653400031d8d9f","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["5b720760c44b7600034b79a7","5b720760c44b7600034b79a8","5b720760c44b7600034b79a9","5b720760c44b7600034b79aa","5b720760c44b7600034b79ab","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5b720760c44b7600034b79ac","5b720760c44b7600034b79ad","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"API V6","version_clean":"1.4.0","version":"1.4"},"category":{"_id":"5b720760c44b7600034b79ac","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"5b720760c44b7600034b7a08","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":6,"slug":"advanced-techniques","title":"Advanced Techniques"},"user":"5b0b7a46a26e6400036604fd","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2019-02-19T00:59:37.387Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":22,"body":"iOS location updates are provided through the [CoreLocation](https://developer.apple.com/documentation/corelocation?language=objc) library. This can provide location updates to your app when it is running in the foreground, the background and when terminated.\n\nYou will need to create a location services class that contains an instance of the `CLLocationManager` class. The `CLLocationManager` instance should be instantiated and registered for the required level of location updates every time the app is run. Once the app has been registered for location updates they will be provided through the `CLLocationManagerDelegate` you specify.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Note\",\n  \"body\": \"Location updates provided when your app is terminated will launch your app in the background, calling the application `willFinishLaunchingWithOptions:` and `didFinishLaunchingWithOptions:` methods. To properly handle the location you must ensure the location manager is created and registered for updates during this launch.\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// location services should implement location manager delegate\\n:::at:::interface LocationServices () <CLLocationManagerDelegate>\\n\\n@property (strong, nonatomic) CLLocationManager *locationManager;\\n\\n// Setup location manager\\n- (void)setupLocationManager {\\n    self.locationManager = [[CLLocationManager alloc] init];\\n    self.locationManager.delegate = self;\\n}\\n\\n\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"class LocationManager: NSObject, CLLocationManagerDelegate {\\n \\n    var locationManager: CLLocationManager!\\n    \\n    // setup location manager\\n    func setupLocationManager() {\\n        locationManager = CLLocationManager()\\n        locationManager.delegate = self\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n## Registering for updates\nApple provides a guide to choosing your authorization level and registering for the appropriate location updates [here](https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services?language=objc).\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Note\",\n  \"body\": \"Make sure that you have added all the correct information to your app's info.plist to support location updates, like the `NSUsageLocationDescriptions` that are used to explain why you need locations to the user. If you want locations in the background you will also need to check the location updates box in the background modes capabilities for your app.\"\n}\n[/block]\n## Passing location updates to Sailthru Mobile\nOnce you have registered and your app is receiving location updates they will arrive through the `CLLocationManagerDelegate` methods. You can then pass the location using the `Carnival` `updateLocation:` method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {\\n    CLLocation *location = [locations lastObject];\\n    [Carnival updateLocation:location];\\n}\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\\n    let location = locations.last\\n    Carnival.updateLocation(location)\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n## Background locations\nIn order to receive background locations you will need to add some additional capabilities to your app. Apple provides a guide for this [here](https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/handling_location_events_in_the_background?language=objc). Most users are reluctant to allow apps to track them all the time so this option should be implemented carefully.","excerpt":"A guide to location updates on iOS","slug":"ios-location-updates","type":"basic","title":"iOS: Location Updates"}

iOS: Location Updates

A guide to location updates on iOS

iOS location updates are provided through the [CoreLocation](https://developer.apple.com/documentation/corelocation?language=objc) library. This can provide location updates to your app when it is running in the foreground, the background and when terminated. You will need to create a location services class that contains an instance of the `CLLocationManager` class. The `CLLocationManager` instance should be instantiated and registered for the required level of location updates every time the app is run. Once the app has been registered for location updates they will be provided through the `CLLocationManagerDelegate` you specify. [block:callout] { "type": "info", "title": "Note", "body": "Location updates provided when your app is terminated will launch your app in the background, calling the application `willFinishLaunchingWithOptions:` and `didFinishLaunchingWithOptions:` methods. To properly handle the location you must ensure the location manager is created and registered for updates during this launch." } [/block] [block:code] { "codes": [ { "code": "// location services should implement location manager delegate\n@interface LocationServices () <CLLocationManagerDelegate>\n\n@property (strong, nonatomic) CLLocationManager *locationManager;\n\n// Setup location manager\n- (void)setupLocationManager {\n self.locationManager = [[CLLocationManager alloc] init];\n self.locationManager.delegate = self;\n}\n\n", "language": "objectivec" }, { "code": "class LocationManager: NSObject, CLLocationManagerDelegate {\n \n var locationManager: CLLocationManager!\n \n // setup location manager\n func setupLocationManager() {\n locationManager = CLLocationManager()\n locationManager.delegate = self\n }\n}", "language": "swift" } ] } [/block] ## Registering for updates Apple provides a guide to choosing your authorization level and registering for the appropriate location updates [here](https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services?language=objc). [block:callout] { "type": "info", "title": "Note", "body": "Make sure that you have added all the correct information to your app's info.plist to support location updates, like the `NSUsageLocationDescriptions` that are used to explain why you need locations to the user. If you want locations in the background you will also need to check the location updates box in the background modes capabilities for your app." } [/block] ## Passing location updates to Sailthru Mobile Once you have registered and your app is receiving location updates they will arrive through the `CLLocationManagerDelegate` methods. You can then pass the location using the `Carnival` `updateLocation:` method. [block:code] { "codes": [ { "code": "- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {\n CLLocation *location = [locations lastObject];\n [Carnival updateLocation:location];\n}", "language": "objectivec" }, { "code": "func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\n let location = locations.last\n Carnival.updateLocation(location)\n}", "language": "swift" } ] } [/block] ## Background locations In order to receive background locations you will need to add some additional capabilities to your app. Apple provides a guide for this [here](https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/handling_location_events_in_the_background?language=objc). Most users are reluctant to allow apps to track them all the time so this option should be implemented carefully.