简体   繁体   English

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

This is my source code. 这是我的源代码。 and I'm Using Xcode 9 and swift 4.0 我正在使用Xcode 9和Swift 4.0

When I build and run, debug area print this "Could not inset legal attribution from corner 4", and Using GPS alert automatic dismiss. 当我生成并运行时,调试区域将打印“无法从第4个角插入合法属性”,并使用GPS警报自动关闭。 So I can't accept using GPS allow. 所以我不能接受使用GPS允许。

I also complete info.plist setting like this. 我也像这样完成info.plist设置。

Privacy - Location Always and When In Use Usage Description Privacy - Location Always Usage Description Privacy - Location When In Use Usage Description. 隐私-始终使用时的位置使用情况说明隐私-始终使用时的位置使用情况说明隐私-使用时的位置在使用情况说明。

What is the problem? 问题是什么?

Make CLLocationManager as global instance var - 将CLLocationManager设为全局实例var-

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

This will prevent alert from dismissing automatically. 这将防止警报自动关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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