0
我想我的應用程序採取積極和背景模式的位置(如果僅使用NSLocationAlwaysUsageDescription的許可,則myLocationButton不顯示)。 我在Info.plist中設置:只顯示一個位置的權限警報
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>UIBackgroundModes</key>
而添加的MapViewController
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestAlwaysAuthorization()
但是,應用程序啓動時,只顯示第一位置的權限警報時,並且應用程序後出現的第二個警報的權限重新開放。
更新:
override func viewDidLoad() {
super.viewDidLoad()
...
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
viewMap.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
self.startLocationUpdates()
...
}
func startLocationUpdates() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.activityType = CLActivityType.automotiveNavigation
self.locationManager.distanceFilter = distanceFilterMetr
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let myLocation: CLLocation = change?[NSKeyValueChangeKey.newKey] as! CLLocation
viewMap.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: observeZoom)
mapRoute.coordinateLatitude = myLocation.coordinate.latitude
mapRoute.coordinateLongitude = myLocation.coordinate.longitude
viewMap.delegate = self
viewMap.settings.myLocationButton = true
viewMap.settings.compassButton = true
didFindMyLocation = true
}
您只需要請求「始終」授權。這使您的應用有權在前臺和後臺使用位置信息。 – Paulw11
@ Paulw11我寫了如果只使用NSLocationAlwaysUsageDescription權限,然後myLocationButton不顯示(位置更新但按鈕不顯示)iOS 10.3 swift 3 – Roman
你如何決定是否顯示該按鈕?我可以向你保證,總是身份驗證是前臺和後臺位置所需要的。 – Paulw11