在 iOS 中找不到位置并自动关闭警报

Can't find location and Alert dismiss automatic in iOS

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapKit: MKMapView!

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        mapKit.setRegion(region, animated: true)
        self.mapKit.showsUserLocation = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()
    }
}

这是我的源代码。我正在使用 Xcode 9 和 swift 4.0

当我构建和 运行 时,调试区域打印此 "Could not inset legal attribution from corner 4",并使用 GPS 警报自动解除。所以我不能接受使用 GPS 允许。

我也是这样完成info.plist设置的

隐私 - 位置始终和使用时使用说明 隐私 - 位置始终使用说明 隐私 - 使用时的位置使用说明。

问题是什么?

将 CLLocationManager 作为全局实例变量 -

    @IBOutlet weak var mapKit: MKMapView!
    let manager = CLLocationManager()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location = locations[0]
            let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
            let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
            let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
            mapKit.setRegion(region, animated: true)
            self.mapKit.showsUserLocation = true
        }


    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()
    }

这将防止警报自动解除。