I did a lot of research about how to get location services working properly with Swift and iOS8
. I know there are a lot of threads describing the common pitfalls but nothing did work for me.
This is the code of my view controller:
import UIKit
class ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
//#2
var gmaps: GMSMapView?
let locationManager = CLLocationManager()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
var target: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 51.6, longitude: 17.2)
var camera: GMSCameraPosition = GMSCameraPosition(target: target, zoom: 6, bearing: 0, viewingAngle: 0)
gmaps = GMSMapView(frame: CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height))
if let map = gmaps? {
map.camera = camera
map.delegate = self
self.view.addSubview(gmaps!)
map.animateToZoom(10)
}
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
if let map = gmaps? {
map.myLocationEnabled = true
map.settings.myLocationButton = true
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
if let map = gmaps? {
map.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
}
In my info plist I have set the keys NSLocationAlwaysUsageDescription
, NSLocationWhenInUseUsageDescription
and Privacy - Location Usage Description. I also added the CoreLocation
.framework to my linked frameworks and libraries.
BUT I still do not get a popup asking the user for the location permissions. There is no error but also nothing else happening :-(
You should check if the permission is already granted for your app and if so, remove that permission (iOS settings app, privacy).
Another recommendation is to completely deinstall the app from simulator/device.
Here is a code snippet from my appDelegate, it uses requestAlwaysAuthorization
but that shouldn't make a difference here:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
// intialize locationManager
locationManager.delegate = self
locationManager.activityType = CLActivityType.Fitness
locationManager.distanceFilter = 10 // 10m
locationManager.requestAlwaysAuthorization()
// get current location
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
...
}
And this is in my Info.plist
:
<key>NSLocationAlwaysUsageDescription</key>
<string>No tracking without your permission</string>
I also have problems with CLLocationManager. You need request access if state is changed to NotDetermined
. Example:
private var isInitalized = false
private func initLocationManagerIfNescessary() {
if isInitalized { return }
isInitalized = true
locationManager = CLLocationManager()
locationManager.delegate = self
// locationManager.locationServicesEnabled
locationManager.desiredAccuracy = kCLLocationAccuracyBest
let Device = UIDevice.currentDevice()
let iosVersion = NSString(string: Device.systemVersion).doubleValue
let iOS8 = iosVersion >= 8
if iOS8 {
//locationManager.requestAlwaysAuthorization() // add in plist NSLocationAlwaysUsageDescription
locationManager.requestWhenInUseAuthorization() // add in plist NSLocationWhenInUseUsageDescription
} else {
locationManager.startUpdatingLocation()
}
}
internal func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case CLAuthorizationStatus.AuthorizedWhenInUse:
locationManager.startUpdatingLocation()
case CLAuthorizationStatus.NotDetermined:
// After first request status may be not autorized, do request access again
locationManager.requestWhenInUseAuthorization()
default: break
}
}
我将NSLocationWhenInUseUsageDescription
放在Test .plist文件中。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.