ARTICLES
CLLocationManager and tvOS
How to access the user’s location with CLLocationManager on tvOS.
- Add the key
NSLocationWhenInUseUsageDescriptionto your Info.plist. Also add a default value that will be displayed in the prompt to the user. - Add the key
CFBundleDisplayNameto your Info.plist. Also add a default value that will be displayed as the App name in the home screen and in the prompt. - Localize your prompt and display name by adding a InfoPlist.strings file. This file can be located in the same directory as your Localizable.strings file.
- Add
NSLocationWhenInUseUsageDescriptionandCFBundleDisplayNameas keys and localize their values.
- Add
According to Apple you should create a strong reference to CLAuthorizationStatus and implement the CLLocationManagerDelegate. Below is some sample code:
let authorizationStatus: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if (authorizationStatus == CLAuthorizationStatus.Restricted || authorizationStatus == CLAuthorizationStatus.Denied) {
self.locationManager = nil
} else {
self.locationManager = CLLocationManager()
}
if (self.locationManager != nil) {
self.locationManager!.delegate = self
//Request Permission to Use Location Services
if (authorizationStatus == CLAuthorizationStatus.NotDetermined) {
self.locationManager!.requestWhenInUseAuthorization()
}
self.locationManager!.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager!.requestLocation()
}
Documentation:
Did you find this post helpful? Support independent development and improve your sleep by downloading White Noise and Deep Sleep Sounds free from the App Store.
Tested with:
Xcode Version 7.2.1
tvOS 9.1.1
Swift 2.1.1