[英]iOS 9 [locationManager.requestWhenInUseAuthorization()] alert view disappears quickly after appearing
I have a pretty basic app configured. 我配置了一个非常基本的应用程序。 It uses a navigation controller, along with a view controller that i have configured as my map view controller.
它使用导航控制器,以及我已配置为地图视图控制器的视图控制器。
When the app launches, it launches to the Root View Controller, and from there, i have a button that's configured to segue to the map view controller and display the map. 当应用启动时,它会启动到Root View Controller,然后从那里启动一个按钮,该按钮配置为可以连接到Map View Controller并显示地图。 When this happens, the alert view will display, but before i can select allow/don't allow, it disappears and the map loads.
发生这种情况时,将显示警报视图,但是在我可以选择允许/不允许之前,它会消失并且地图会加载。
Why does the alert view disappear on its own? 为什么警报视图会自行消失?
I've configured the info.plist file accordingly, added the necessary frameworks and added the appropriate delegates. 我已经相应地配置了info.plist文件,添加了必要的框架并添加了适当的委托。
Below is the code i have configured: 下面是我配置的代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations[0]
let latitude = userLocation.coordinate.latitude
let longitude = userLocation.coordinate.longitude
let latDelta: CLLocationDegrees = 0.01
let lonDelta: CLLocationDegrees = 0.01
let coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let region: MKCoordinateRegion = MKCoordinateRegionMake(coordinate, span)
map.setRegion(region, animated: true)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Any help will be greatly appreciated! 任何帮助将不胜感激! Thanks in advance!
提前致谢!
Declare your locationManager ivar outside of viewDidLoad. 在viewDidLoad之外声明您的locationManager ivar。 It is being deallocated after viewDidLoad completes because nothing has a strong reference to it.
viewDidLoad完成后,将对其进行重新分配,因为没有任何内容对其有很强的引用。 You should probably also only start scanning after your user has granted access.
您可能还应该仅在用户授予访问权限后才开始扫描。
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
let authorizationStatus = CLLocationManager.authorizationStatus()
if (authorizationStatus == CLAuthorizationStatus.NotDetermined) {
locationManager.requestWhenInUseAuthorization()
} else if (authorizationStatus == CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.startUpdatingLocation()
}
} }
Because you're doing it wrong. 因为你做错了。 The CLLocationManagerDelegate protocol has a method called didChangeAuthorizationStatus.
CLLocationManagerDelegate协议具有一个称为didChangeAuthorizationStatus的方法。 You should check there if the user allowed or not the app to use location.
您应该在此处检查用户是否允许该应用使用位置。 After checking that, you should call startUpdatingLocation.
检查之后,您应该调用startUpdatingLocation。 Be sure to do all UI updates required by this code using dispatch_async on the main queue as well.
确保还使用主队列上的dispatch_async完成此代码所需的所有UI更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.