简体   繁体   中英

How to keep location service request alert box stays appear

I'm having strange problem with UITableViewController. I'm requesting location service authroization inside viewDidLoad method, and I could see alertbox appear. However, this alertbox stays appear for couple of seconds then disappears. Why is this happening?

 override func viewDidLoad() {
   let locationManager = CLLocationManager()
   locationManager.requestAlwaysAuthorization() 
 }

I tried this code inside the UIViewController that loads this UITableViewController and encountered different problem. This time, only push notification service request alertbox is displayed and location service request is entirely ignored. I'm guessing it is because push notification service request is not from me but from ios, and my location service request got overwritten by the ios notification request. It's my assumption correct or is there any explination for this behaviour?

Your location manager is going out of existence because it is a local variable. Make it a persistent property:

let locationManager = CLLocationManager()
override func viewDidLoad() {
    // ...
}

Also I'm not sure whether it's a good idea to do this in viewDidLoad . The view is not yet in the interface; in fact, there may be no interface yet.

But there are a lot of other things wrong with your code. You may already have authorization, or authorization may already have been denied , in which case there is no point requesting it. In general authorization requests are a much more elaborate business than your simple-minded code makes out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM