我做了很多关于如何使用Swift和iOS8
使位置服务正常工作的研究。我知道有很多线程描述了常见的陷阱,但没有任何对我有用。
这是我的视图控制器的代码:
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()
}
}
}
}
在我的信息plist中,我设置了密钥NSLocationAlwaysUsageDescription
,NSLocationWhenInUseUsageDescription
和隐私 - 位置使用说明。我还将CoreLocation
。框架添加到我的链接框架和库中。
但我仍然没有弹出一个询问用户位置权限的弹出窗口。没有错误,但也没有其他事情发生: - (
答案 0 :(得分:1)
您应该检查是否已经为您的应用授予了权限,如果是,请删除该权限(iOS设置应用,隐私)。
另一个建议是从模拟器/设备中完全卸载应用程序。
以下是我的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]!) {
...
}
这是在Info.plist
:
<key>NSLocationAlwaysUsageDescription</key>
<string>No tracking without your permission</string>
答案 1 :(得分:0)
我也遇到CLLocationManager问题。如果状态更改为NotDetermined
,则需要请求访问权限。例如:
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
}
}
答案 2 :(得分:0)
我将NSLocationWhenInUseUsageDescription
放在Test .plist文件中。