CLLocationManager 授权消息

CLLocationManager authorization message

你好,我正在对我的应用程序使用 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 个按钮允许和不允许。如果我单击不允许按钮并关闭应用程序,当我再次打开它时,我不会再收到提示消息。

如何让用户每次打开应用程序时都显示此提示信息?谢谢

一旦用户拒绝,就没有办法做到这一点,你必须显示一个对话框向用户解释 he/she 必须转到“设置”并手动允许功能。

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

  1. 每次都提示alert不是一个有效的方法。
  2. 作为替代方案,您可以仅在 位置服务 处于这种情况时显示警报 禁用 或“不允许”最初。
  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)!)*/
    
                }
            }
        }
    }