[英]Cannot get location services working with Swift and iOS8 with NSLocationWhenInUseUsageDescription
I did a lot of research about how to get location services working properly with Swift and iOS8
. 我做了很多有关如何使定位服务与Swift和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. 在我的信息NSLocationAlwaysUsageDescription
,我设置了键NSLocationAlwaysUsageDescription
, NSLocationWhenInUseUsageDescription
和Privacy-Location NSLocationWhenInUseUsageDescription
Description。 I also added the CoreLocation
.framework to my linked frameworks and libraries. 我还将CoreLocation
.framework添加到了链接的框架和库中。
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). 您应该检查是否已为您的应用授予该权限,如果已授予,请删除该权限(iOS设置应用,隐私)。
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: 这是我的appDelegate的代码片段,它使用requestAlwaysAuthorization
但是在这里应该没有什么不同:
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
: 这是在我的Info.plist
:
<key>NSLocationAlwaysUsageDescription</key>
<string>No tracking without your permission</string>
I also have problems with CLLocationManager. CLLocationManager也有问题。 You need request access if state is changed to NotDetermined
. 如果状态更改为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文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.