Ios CLLocationManager授权消息

Ios CLLocationManager授权消息,ios,swift,cllocationmanager,Ios,Swift,Cllocationmanager,您好,我正在将CLLocation用于我的应用程序,我已初始化我的CLLocationManager,如下所示: func initLocationManager(){ locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters let auth

您好,我正在将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和nsLocationWhenUsageDescription密钥

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


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

一旦用户拒绝,您就没有办法这样做,您必须向用户显示一个对话框,说明他/她必须进入设置并手动启用该功能。

此消息要求您允许使用您设备的GPS,并尝试获取设备的位置,这就是为什么在手机上非常需要GPS的原因。第二,您不想显示它,您可以使用NSUseDefaults创建一个条件并存储一个键,然后不要调用locationmanager startupdatinglocation方法。这是唯一一种不再显示的方法,否则每次都会显示

  • 每次提示警报不是有效的方法
  • 另一种选择是,只有在启用了位置服务时,您才能显示警报 最初禁用了或“不允许”
  • 首先是以下代码提示警报,当位置服务被禁用时,会出现自定义警报

    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)!)*/
    
                }
            }
        }
    }
    

  • 据我所知,这是不可能的。此系统提示仅第一次显示。那它就不会再出现了。您可以显示自定义警报,告诉用户转到“设置”并为您的应用启用位置服务。这并不完全准确。如果用户拒绝,对话框将不再显示,
    requestPermission
    将返回
    kclauauthorizationstatusdenied
    。然后你不能再显示该对话框-你必须要求用户在设置中手动设置。Michal只是先仔细阅读问题,他想第一次,然后不授予权限,然后再次打开应用程序,屏幕上不再显示提示。我如何使此提示消息在用户每次打开应用程序时出现?--恐怕你需要仔细阅读。OP正在询问下次如何再次显示它,即使它之前已被拒绝。如果我们未请求授权,则这将如何让authstate=CLLocationManager.authorizationStatus()第一次显示为.AuthorizationWhenInUse或.AuthorizationDalWays?第一次的状态为notdetermined@PanayiotisIrakleous是的,你是对的。请检查更新的代码。并让我知道它是否完全满足您的需要。现在,第二次我无法获取位置,因为位置管理器永远不会得到InstanceAte当您在位置管理器第一次实例化时打开应用程序,然后会出现提示。你能让我解释一下在你的哪种情况下它不是实例化的吗。