[英]App doesn't ask location service permission WhenInUse when installation?
我在我的应用程序中使用谷歌地图。我已经在info.plist中设置了它
隐私权-使用时的位置用法说明
并且在我的代码(HomeScreen)中,我也像这样检查:
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
} else{
let alertController = UIAlertController(title: "Oops !!" , message: "Location service seems to be disabled. Please enable from Settings -> Privacy ->LocationService.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
但是在首次安装该应用程序时,它不会征求许可。 任何帮助都会有所帮助。
您正在设置Info.plist
键,以便在用户使用应用程序时(即,当应用程序位于前台时)访问位置,但是在您的代码中,只要应用程序在运行(即始终),您都在请求权限。
您需要决定要哪个。 如果您希望始终能够访问用户的位置,请更改Info.plist
键。 如果您想在应用程序处于前台时访问用户的位置,则将权限请求更改为requestWhenInUseAuthorization()
。
import CoreLocation
class AppDelegate: CLLocationManagerDelegate{
var locationManager: CLLocationManager!
var currentCoordinate: CLLocationCoordinate2D?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.setupLocationManager()
return true
}
func setupLocationManager(){
locationManager = CLLocationManager()
locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager?.startUpdatingLocation()
}
// Below method will provide you current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if currentCoordinate == nil {
currentCoordinate = locations.last?.coordinate
locationManager?.stopMonitoringSignificantLocationChanges()
let locationValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locationValue)")
//currentCoordinate use this location Coordinate in your whole app.
locationManager?.stopUpdatingLocation()
}
}
// Below Mehtod will print error if not able to update location.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error")
}
如果根据我的回答有疑问,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.