【问题标题】:iOS 9 [locationManager.requestWhenInUseAuthorization()] alert view disappears quickly after appearingiOS 9 [locationManager.requestWhenInUseAuthorization()] 警报视图出现后迅速消失
【发布时间】:2016-01-02 11:24:07
【问题描述】:

我配置了一个非常基本的应用程序。它使用导航控制器,以及我已配置为地图视图控制器的视图控制器。

当应用程序启动时,它会启动到根视图控制器,然后从那里,我有一个按钮,该按钮被配置为连接到地图视图控制器并显示地图。发生这种情况时,将显示警报视图,但在我选择允许/不允许之前,它会消失并加载地图。

为什么警报视图会自行消失?

我已经相应地配置了 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.
}


}

任何帮助将不胜感激!提前致谢!

【问题讨论】:

    标签: ios swift mapkit core-location


    【解决方案1】:

    在 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()
      }
    

    }

    【讨论】:

    • 使用“var locationManager:CLLocationManager”会更好吗? (带问号)或“var locationManager:CLLocationManager!” (带感叹号)代替?
    • 如果你想要求一个 locationManager 被初始化。是的,你可能会这样做。
    • 为什么建议先在viewDidLoad之外初始化?为什么我不能只在 viewDidLoad 之外使用 var locationManager = CLLocationManager()?
    • 一个重要的区别是您没有在 viewDidLoad 之外初始化 locationManager。您正在 viewDidLoad 中对其进行初始化。您在类级别声明一个强引用属性。 developer.apple.com/library/prerelease/ios/documentation/Swift/…
    • 这让我大吃一惊,拯救了我的皮肤。谢谢@Keller!
    【解决方案2】:

    因为你做错了。 CLLocationManagerDelegate 协议有一个名为 didChangeAuthorizationStatus 的方法。您应该在那里检查用户是否允许应用程序使用位置。检查后,您应该调用 startUpdatingLocation。请务必在主队列上使用 dispatch_async 完成此代码所需的所有 UI 更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多