【问题标题】:I have a problem on running my self coded app on Swift 10.2我在 Swift 10.2 上运行我的自编码应用程序时遇到问题
【发布时间】:2019-08-23 03:33:21
【问题描述】:

Xcode 说有一个 Thread 1: signal SIGABRT 。它还说 libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 。我是初学者,所以请“轻松”回复;)

//  ViewController.swift

import UIKit
import MapKit
import CoreLocation

class MapScreen: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        checkLocationServices()
    }

    func setupLocationManager() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }


    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuthorization()
        }else{
            // show alert letting the user know he has to turn them on.
        }
    }


    func checkLocationAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            mapView.showsUserLocation = true
            break
        case .denied:
            // show alert instructing how to turn on permissions
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        case .restricted:
            // show an alert letting them know what's up
            break
        case .authorizedAlways:
            break
        }
    }
}


    extension MapScreen: CLLocationManagerDelegate {
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            // later
            }
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            // later
        }
    }

【问题讨论】:

  • 您是否连接了情节提要中的mapView 插座? (你在 Xcode 中看到属性旁边的实心圆圈了吗?)。你看到位置权限提示了吗?您是否已将位置使用说明添加到您的info.plist?应该有与控制台中显示的异常相关的错误消息
  • @Paulw11 是的,除了实心圆圈外,一切似乎都很好。它没有被填满。但是我连接了它...
  • 您收到错误消息,因为您告诉应用程序在地图上显示用户的当前位置。但该应用尚未准备好这样做,因为您不允许该应用访问地图视图的委托属性。
  • @ElTomato 我该如何解决这个问题?
  • 搜索 showUserLocation 并阅读其他主题。

标签: ios swift mapkit sigabrt


【解决方案1】:

要填充圆圈(在将 UI 控件连接到情节提要的 @IBOutlet 旁边),您需要打开 .swift 文件和情节提要,尝试这些以确保其实际连接

  • 打开storyboard文件,再打开MapScreen.swift文件,连接器如果有连接就填上
  • 打开故事板文件并单击以显示Assistant Editor,因此故事板和MapScreen.swift 文件同时打开,同时确保在Identity Inspector 中将Class 设置为MapScreen,如下面的屏幕截图所示

我也会列出一些关于 MKMapView 和 LocationManager 的建议


  • locationManager.requestWhenInUseAuthorization() 将不起作用,如果Info.plist 没有以下一项或多项使用说明

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Message for AlwaysAndWhenInUseUsageDescription</string>
    
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Message for AlwaysUsageDescription</string>
    
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Message for WhenInUseUsageDescription</string>
    
  • 一旦获得使用位置服务的权限,需要询问locationManager.startUpdatingLocation(),这样您将通过CLLocationManagerDelegate获得位置更新

  • locationManager(_:didUpdateLocations:) 内接收位置更新,为了能够在地图上看到用户位置,我们需要将地图区域设置为该位置,使用mapView.setRegion

这是更新后的类

class MapScreen: UIViewController {

    @IBOutlet var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupLocationManager()
    }

    func setupLocationManager() {
        guard CLLocationManager.locationServicesEnabled() else {
            // show alert letting the user know he has to turn them on.
            print("Location Servies: Disabled")
            return
        }

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        checkLocationAuthorization()
    }

    func checkLocationAuthorization(authorizationStatus: CLAuthorizationStatus? = nil) {
        switch (authorizationStatus ?? CLLocationManager.authorizationStatus()) {
        case .authorizedAlways, .authorizedWhenInUse:
            locationManager.startUpdatingLocation()
            mapView.showsUserLocation = true
        case .restricted, .denied:
            // show alert instructing how to turn on permissions
            print("Location Servies: Denied / Restricted")
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
        }
    }
}


extension MapScreen: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.count > 1 ? locations.sorted(by: { $0.timestamp < $1.timestamp }).last : locations.first else { return }

        let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let region = MKCoordinateRegion(center: location.coordinate, span: span)
        mapView.setRegion(region, animated: true)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        self.checkLocationAuthorization(authorizationStatus: status)
    }

}

结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2020-08-17
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多