应用程序不询问位置服务权限何时在安装时使用?

时间:2017-03-14 04:40:47

标签: ios swift cllocationmanager locationmanager location-services

我在我的应用中使用谷歌地图。我已在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)
    }

但是第一次安装应用时它没有请求权限。任何帮助都会有所帮助。

3 个答案:

答案 0 :(得分:2)

您正在设置Info.plist密钥,以便在用户使用应用程序时访问该位置(即,当它位于前台时),但在您的代码中,您在应用程序运行时请求权限(即总是)。

你需要决定你想要的。如果您希望始终能够访问用户的位置,请更改Info.plist密钥。如果您想在应用程序位于前台时访问用户的位置,请将权限请求更改为requestWhenInUseAuthorization()

答案 1 :(得分:0)

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")
}

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

答案 2 :(得分:0)

从iOS 10开始,我们需要在.plist文件中提供位置权限,就像跟随enter image description here

一样

并在设置中检查您应用的位置服务是否启用。设置 - >你的应用名称 - >允许位置访问。永远不应该被选中。

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