【问题标题】:CLLocationManager authorization messageCLLocationManager 授权消息
【发布时间】:2015-07-24 10:47:34
【问题描述】:

您好,我正在对我的应用程序使用 CLLocation,并且我已经像这样初始化了我的 CLLocationManager:

func initLocationManager(){    
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    let authstate = CLLocationManager.authorizationStatus()
    if(authstate == CLAuthorizationStatus.NotDetermined || authstate == CLAuthorizationStatus.Denied){
        println("Not Authorised")
        locationManager.requestWhenInUseAuthorization()
    }
    locationManager.startUpdatingLocation() 
}

我还在我的 plist 中添加了 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription 键。

当我第一次打开我的应用程序时,我会收到我的应用程序想要访问位置的提示消息,它有 2 个按钮允许和不允许。如果我单击不允许按钮并关闭应用程序,当我再次打开它时,我不会再次收到提示消息。

如何在每次用户打开应用程序时显示此提示消息?谢谢

【问题讨论】:

  • 据我所知这是不可能的。此系统提示仅在第一次显示。然后就不会再出现了。您可以显示自定义警报,告诉用户转到设置并为您的应用启用位置服务。

标签: ios swift cllocationmanager


【解决方案1】:
  1. 每次都提示警报不是有效的方法。
  2. 作为替代方案,您可以仅在 Location Service 启用的情况下显示警报 最初禁用或“不允许”。
  3. 首先遵循代码提示警报,然后在 位置服务 禁用

    时发出自定义警报
    import UIKit
    
    import CoreLocation
    
    class ViewController: UIViewController,CLLocationManagerDelegate {    
    
        var locationManager = CLLocationManager()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            initLocationManager()
    
    
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func initLocationManager(){
    
            let status = CLLocationManager.authorizationStatus()
    
            if(status == CLAuthorizationStatus.NotDetermined) {
                locationManager = CLLocationManager()
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    
                let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue
    
                if iosVersion >= 8.0 {
                    //For Foreground
                    locationManager.requestWhenInUseAuthorization()
                }
                locationManager.startUpdatingLocation()
    
            } else {
                if(status != CLAuthorizationStatus.AuthorizedWhenInUse) {
    
    
                    var alert = UIAlertView(title: "Location", message: "Please turn on Location Services", delegate: nil, cancelButtonTitle: "Cancel")
                    alert.addButtonWithTitle("Open Setting")
                    alert.show()
    
                    /*Add Action on Open Setting alertbutton to directly open settings in iOS 8 and later
    
                    ->    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)*/
    
                }
            }
        }
    }
    

【讨论】:

  • 如果我们不请求授权,第一次 let authstate = CLLocationManager.authorizationStatus() 将如何给出 .AuthorizeWhenInUse 或 .AuthorizedAlways?
  • 第一次状态未确定
  • @PanayiotisIrakleous 是的,你是对的。请检查更新的代码。如果它完全满足您的需要,请告诉我。
  • 现在我第二次无法获取位置,因为位置管理器从来没有实例化
  • 当您第一次打开应用程序时,位置管理器正在实例化,然后提示即将到来。您能否让我解释一下在您的哪种情况下它没有实例化。
【解决方案2】:

一旦用户拒绝,就无法做到这一点,您必须显示一个对话框,向用户解释他/她必须转到“设置”并手动允许该功能。

【讨论】:

  • Michal 只是先仔细阅读了这个问题,他想要第一次然后不给予许可,然后他再次打开应用提示,屏幕上不再显示。
  • 如何让每次用户打开应用时都出现这个提示信息? -- 恐怕需要仔细阅读. OP 正在询问如何在下次 再次 显示它,即使它之前被拒绝
【解决方案3】:

此消息要求您获得使用设备 GPS 的权限,并尝试获取设备的位置,这就是为什么非常需要 GPS。其次,您不想显示它,您可以使用 NSUseDefaults 创建一个条件并存储一个密钥,然后不要调用方法 locationmanager startupdatinglocation。这是不再显示它的唯一方法,否则它会每次都显示。

【讨论】:

  • 这并不完全准确。如果用户拒绝,对话框将不再显示,requestPermission 将返回kCLAuthorizationStatusDenied。然后您无法再次显示该对话框 - 您必须要求用户在“设置”中手动设置它。
猜你喜欢
  • 2018-08-13
  • 2014-08-31
  • 2016-03-07
  • 1970-01-01
  • 2019-05-16
  • 2016-11-02
  • 1970-01-01
  • 2013-10-02
  • 2017-06-08
相关资源
最近更新 更多