【问题标题】:Location authorization with Mapkit keeps throwing this error: (0x14) "Unable to insert COPY_SEND"Mapkit 的位置授权不断抛出此错误:(0x14)“无法插入 COPY_SEND”
【发布时间】:2017-12-10 22:13:11
【问题描述】:

Xcode 9.1 斯威夫特 4

这是我在控制台中遇到的错误: [常见]_BSMachError:端口6507; (os/kern) 无效能力 (0x14) “无法插入 COPY_SEND”

我正在尝试让我的地图在模拟器中显示用户位置(自定义位置)并居中并放大该位置。我已经实现了

隐私 - 始终定位和使用时使用说明

隐私 - 使用时的位置使用说明

隐私 - 位置始终使用说明

在 info.plist 中所有的字符串值。

这是我的 MapVC 视图控制器中的代码:

import UIKit
import MapKit
import CoreLocation

class MapVC: UIViewController {


    @IBOutlet weak var mapView: MKMapView!

    var locationManager = CLLocationManager()
    let authorizationStatus = CLLocationManager.authorizationStatus()
    let regionRadius: Double = 1000

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self
        locationManager.delegate = self

        configureLocationServices()
        mapView.showsUserLocation = true


    }


}

extension MapVC: MKMapViewDelegate {
    func centerMapOnUserLocation() {
        guard let coordinate = locationManager.location?.coordinate else { return }
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }

}

extension MapVC: CLLocationManagerDelegate {
    func configureLocationServices() {
        if authorizationStatus == .notDetermined {
            locationManager.requestAlwaysAuthorization()
        } else {
            return
        }


        }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        centerMapOnUserLocation()


    }
}

运行应用程序时,会出现“访问位置”弹出窗口,我选择“始终允许”或“仅在使用应用程序时”。弹出窗口消失,在检查模拟器的隐私设置时,它反映了我的选择。然而,在那之后什么也没有发生。我怀疑守卫让坐标没有返回任何坐标,所以它是“否则 {return} 正在被调用并可能停在那里?现在这是有趣的部分。如果我进入设置/隐私并选择其他可用的允许选项,当我回到应用程序,它完全按照我的意愿执行 centerMapOnUserLocation。

我已经尝试了在 SO 和其他网站上可以找到的几乎所有适用的建议。似乎没有任何效果。我已经尝试从头开始重建应用程序 3 次,结果都相同。我刚刚更新到 Xcode 9.2 时遇到了同样的问题。任何帮助将不胜感激。提前致谢!!需要任何其他信息,请告诉我,我会发布。

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    我在 Xcode 9.1Swift 4

    中的完整答案

    首先在您的 ViewController 上添加 MapView 并连接代理并检查情节提要中的 用户位置 复选标记并将 MapView 插座添加到您的 ViewController。快速文件。添加一个名为 getCurrentLocationAction() 的带有 actionMethod 的 Button 以获取 ViewController.swift 文件中的当前位置。如下更新您的 ViewController.swift 文件。

    另外不要忘记在 info.plist 文件中添加两个重要的键。

    1.隐私 - 位置始终使用说明。
    2.隐私-使用时的位置使用说明

    import UIKit
    import MapKit
    
    class ViewController: UIViewController, MKMapViewDelegate {
    
        @IBOutlet weak var mapView: MKMapView!
    
        fileprivate var annotation: MKAnnotation!
        fileprivate var locationManager: CLLocationManager!
        fileprivate var isCurrentLocation: Bool = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            mapView.delegate = self
            mapView.mapType = .standard
    
        }
    
        @IBAction func getCurrentLocationAction(_ sender: Any) {
            if (CLLocationManager.locationServicesEnabled()) {
                if locationManager == nil {
                    locationManager = CLLocationManager()
                }
                locationManager.requestWhenInUseAuthorization()
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.requestAlwaysAuthorization()
                locationManager.startUpdatingLocation()
            }
        }
    
    }
    

    现在创建一个扩展并添加 CLLocationManagerDelegate 方法。

    extension ViewController : CLLocationManagerDelegate{
    
        // MARK: - CLLocationManagerDelegate
    
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
            let location = locations.last
            let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    
            self.mapView.setRegion(region, animated: true)
    
            if self.mapView.annotations.count != 0 {
                annotation = self.mapView.annotations[0]
                self.mapView.removeAnnotation(annotation)
            }
    
            let pointAnnotation = MKPointAnnotation()
            pointAnnotation.coordinate = location!.coordinate
            pointAnnotation.title = ""
            mapView.addAnnotation(pointAnnotation)
        }
    
    }
    

    希望这会有所帮助。快乐编码:)

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      相关资源
      最近更新 更多