繁体   English   中英

应用程序不询问位置服务许可whenInUse在安装时?

[英]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")
}

如果根据我的回答有疑问,请告诉我。

从iOS 10开始,我们需要在.plist文件中授予位置权限,如下所示 在此处输入图片说明

然后只需检查您的应用的位置服务是否在设置中即可。设置->您的应用名称->允许位置访问。 永远不要选择。

从设备中删除您的应用,然后再次重新安装该应用,然后它会询问您位置许可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM