requestWhenInUseAuthorization() not Work in iOS 8 With NSLocationWhenInUseUsageDescription Key in Info.plist

前端 未结 3 1190
花落未央
花落未央 2020-12-19 17:43

I am using iOS SDK 8.1 trying to call requestWhenInUseAuthorization() method to prompt user to grant access to my app. I imported CoreLocation.framework, and added NSLocatio

相关标签:
3条回答
  • 2020-12-19 18:12

    Problem solved. My manager was declared as local var inside viewDidLoad() method, but it should've been a class level property.

    After I moved manager declaration out of viewDidLoad(), my app worked.

    Not sure how exactly manager.requestWhenInUseAuthorization() work behind the scene and why exactly manager defined within viewDidLoad() not work. Hope someone who knows this detail enlighten me.

    0 讨论(0)
  • 2020-12-19 18:20

    I also faced the same problem. I have added two keys together in info.plist.

    NSLocationWhenInUseUsageDescription key is supported in iOS 8.0 and later. If your Info.plist file includes both this key and the NSLocationUsageDescription key, the system uses this key and ignores the NSLocationUsageDescription key.

    Rearranged the code:

    let locationManager: CLLocationManager = CLLocationManager()
      let authorizationStatus = CLLocationManager.authorizationStatus()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        locationManager.delegate = self
        if(authorizationStatus == .Denied)
        {
          print("DENIED")
          locationManager.requestWhenInUseAuthorization()
        }
    
      }
    override func viewWillAppear(animated: Bool) {
    
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
      }
    

    Clean the project and run again.

    0 讨论(0)
  • 2020-12-19 18:30

    Setting these properties in the viewWillAppear instead of the viewDidLoad fixed it for me, thanks!

    override func viewWillAppear(animated: Bool) {
    
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
      }
    
    0 讨论(0)
提交回复
热议问题