iOS 9 [locationManager.requestWhenInUseAuthorization()] alert视图出现后很快消失
iOS 9 [locationManager.requestWhenInUseAuthorization()] alert view disappears quickly after appearing
我配置了一个非常基本的应用程序。它使用导航控制器,以及我配置为地图视图控制器的视图控制器。
当应用程序启动时,它启动到根视图控制器,从那里,我有一个按钮配置为继续到地图视图控制器并显示地图。发生这种情况时,将显示警报视图,但在我 select allow/don 不允许之前,它会消失并加载地图。
为什么警报视图会自行消失?
我已经相应地配置了 info.plist 文件,添加了必要的框架并添加了适当的委托。
下面是我配置的代码:
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.
}
}
任何帮助将不胜感激!提前致谢!
在 viewDidLoad 之外声明您的 locationManager ivar。它在 viewDidLoad 完成后被释放,因为没有任何东西对它有强引用。您可能还应该只在您的用户授予访问权限后才开始扫描。
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()
}
}
因为你做错了。 CLLocationManagerDelegate 协议有一个名为 didChangeAuthorizationStatus 的方法。您应该在那里检查用户是否允许该应用程序使用位置。检查之后,您应该调用 startUpdatingLocation。请务必在主队列上也使用 dispatch_async 执行此代码所需的所有 UI 更新。
我配置了一个非常基本的应用程序。它使用导航控制器,以及我配置为地图视图控制器的视图控制器。
当应用程序启动时,它启动到根视图控制器,从那里,我有一个按钮配置为继续到地图视图控制器并显示地图。发生这种情况时,将显示警报视图,但在我 select allow/don 不允许之前,它会消失并加载地图。
为什么警报视图会自行消失?
我已经相应地配置了 info.plist 文件,添加了必要的框架并添加了适当的委托。
下面是我配置的代码:
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.
}
}
任何帮助将不胜感激!提前致谢!
在 viewDidLoad 之外声明您的 locationManager ivar。它在 viewDidLoad 完成后被释放,因为没有任何东西对它有强引用。您可能还应该只在您的用户授予访问权限后才开始扫描。
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()
}
}
因为你做错了。 CLLocationManagerDelegate 协议有一个名为 didChangeAuthorizationStatus 的方法。您应该在那里检查用户是否允许该应用程序使用位置。检查之后,您应该调用 startUpdatingLocation。请务必在主队列上也使用 dispatch_async 执行此代码所需的所有 UI 更新。