【问题标题】:App doesn't ask location service permission WhenInUse when installation?安装时应用程序不询问位置服务权限WhenInUse?
【发布时间】:2017-03-14 04:40:47
【问题描述】:

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

但第一次安装应用程序时它不会请求许可。任何帮助都会有所帮助。

【问题讨论】:

  • 你加了吗 为了能够检测您是否在开车,我们需要访问您的位置。 在您的 .plist 文件中?
  • 只需在您的 appdelegates.swift 文件中设置此代码
  • @HimanshuMoradiya AppDelagate 中要添加哪些代码??

标签: ios swift cllocationmanager locationmanager location-services


【解决方案1】:

您正在设置Info.plist 键以在用户使用应用程序时访问位置(即当它在前台时),但在您的代码中,您在应用程序运行时请求权限(即始终)。

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

【讨论】:

    【解决方案2】:
    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")
    }
    

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

    【讨论】:

    • @AnushkaMadushan 这行得通,那为什么要给你投反对票?
    • 对不起,我该如何改变它
    • @AnushkaMadushan 接受我的回答。现在它不会撤销你的反对票
    • @AnushkaMadushan 如果您的问题解决了,请批准 answer 。编码愉快。
    【解决方案3】:

    从 iOS 10 开始,我们需要在 .plist 文件中授予位置权限,就像以下一样

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2019-12-31
      • 2019-12-23
      • 2019-01-17
      • 2014-01-06
      • 1970-01-01
      相关资源
      最近更新 更多