Ios 仅显示一个位置权限警报
我希望我的应用程序在活动和后台模式下进行定位(如果仅使用NSLocationAlwaysUsageDescription权限,则myLocationButton不显示)。 我在Info.plist中设置:Ios 仅显示一个位置权限警报,ios,swift,core-location,info-plist,Ios,Swift,Core Location,Info Plist,我希望我的应用程序在活动和后台模式下进行定位(如果仅使用NSLocationAlwaysUsageDescription权限,则myLocationButton不显示)。 我在Info.plist中设置: <key>NSLocationAlwaysUsageDescription</key> <string>$(PRODUCT_NAME) location use</string> <key>NSLocationWhenInUseUsa
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>UIBackgroundModes</key>
但当应用程序启动时,仅显示第一个位置权限警报,而第二个权限警报在应用程序重新打开后显示
更新:
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
}
在类中添加此委托:
CLLocationManagerDelegate
现在在你们班里:
var locationManager:CLLocationManager!
var map = GMSMapView()
var currentLatitude:Double!
var currentLongitude:Double!
之后,在代码中添加以下内容:
override func loadView() {
print("loadView called")
// Enable some map settings
map.isMyLocationEnabled = true
map.settings.myLocationButton = true
map.settings.compassButton = true
map.settings.scrollGestures = true
map.settings.zoomGestures = true
map.delegate = self
view = map
}
override func viewDidLoad() {
super.viewDidLoad()
print("ViewDidLoad called")
// Configuring location manager.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("locationManager function called")
// Fetch current location coordinates
let locValue:CLLocationCoordinate2D = (locationManager.location?.coordinate)!
currentLatitude = locValue.latitude
currentLongitude = locValue.longitude
print("Current Location = \(currentLatitude!), \(currentLongitude!)")
// Zoom to current location
let target = CLLocationCoordinate2D(latitude: currentLatitude!, longitude: currentLongitude!)
map.camera = GMSCameraPosition.camera(withTarget: target, zoom: 17)
locationManager.stopUpdatingLocation()
}
添加此代码后,右键单击Info.plist并“作为源代码打开”。将此添加到您的Info.plist中
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSBluetoothPeripheralUsageDescription</key>
<string></string>
<key>NSCameraUsageDescription</key>
<string></string>
<key>NSContactsUsageDescription</key>
<string></string>
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSMicrophoneUsageDescription</key>
<string></string>
<key>NSMotionUsageDescription</key>
<string></string>
<key>NSPhotoLibraryUsageDescription</key>
<string></string>
<key>NSRemindersUsageDescription</key>
<string></string>
<key>NSSiriUsageDescription</key>
<string></string>
<key>NSSpeechRecognitionUsageDescription</key>
<string></string>
<key>NSVideoSubscriberAccountUsageDescription</key>
<string></string>
LSApplicationQueriesSchemes
谷歌色度
谷歌地图
LSREQUIRESPHONEOS
NSBluetooth外围设备用途说明
NSCameraUsageDescription
NSContactsUsageDescription
NSLocationUsageDescription
NSLocationWhenUse用途说明
N位置始终使用说明
NSM话筒用途说明
NSMotionUsageDescription
NSPhotoLibraryUsageDescription
NSRemindersUsageDescription
NSSIRIUSAGESCRIPTION
NSSpeechRecognitionUsageDescription
NSVideoSubscriberAccountUsageDescription
您只需申请“始终”授权即可。这使您的应用程序有权在前台和后台使用位置。@Paulw11我写的是,如果仅使用NSLocationAlwaysSusageDescription权限,则myLocationButton不显示(位置更新但按钮不显示)iOS 10.3 swift 3您如何决定是否显示该按钮?我可以向您保证,前台和后台位置始终只需要身份验证。@Paulw11请更新我的代码。谢谢请注意,在本教程中,他们实现了didChangeAuthorizationStatus
delegate方法,并使用该方法启用mylocation按钮。您是否实施了该方法并更改了您请求的“始终”授权?
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSBluetoothPeripheralUsageDescription</key>
<string></string>
<key>NSCameraUsageDescription</key>
<string></string>
<key>NSContactsUsageDescription</key>
<string></string>
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSMicrophoneUsageDescription</key>
<string></string>
<key>NSMotionUsageDescription</key>
<string></string>
<key>NSPhotoLibraryUsageDescription</key>
<string></string>
<key>NSRemindersUsageDescription</key>
<string></string>
<key>NSSiriUsageDescription</key>
<string></string>
<key>NSSpeechRecognitionUsageDescription</key>
<string></string>
<key>NSVideoSubscriberAccountUsageDescription</key>
<string></string>