ios - 使用 swift 制作一个获取用户位置的应用程序

标签 ios swift gps

我运行了代码来获取用户的位置,这是我得到的错误。有人可以帮我解决一下吗?

我的代码如下:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var citybtn: UIButton!
@IBOutlet weak var citylbl: UILabel!

let locationManager = CLLocationManager()
var currentLocation : CLLocation!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Used to start getting the users location
    //let locationManager = CLLocationManager()


    // For use when the app is open
    //locationManager.requestAlwaysAuthorization()

    // If location services is enabled get the users location
    //if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest // You can change the locaiton accuary here.
    locationManager.requestWhenInUseAuthorization()



}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    locationAuthStatus()
}

func locationAuthStatus() {

    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {

        currentLocation = locationManager.location
        print(currentLocation.coordinate.latitude)
        print(currentLocation.coordinate.longitude)

    } else {
        locationManager.requestWhenInUseAuthorization()
        locationAuthStatus()
    }
}



@IBAction func buttonpresses(_ sender: Any) {

}



}

Error that gets printed onto console

最佳答案

听起来您没有启用目标的位置更新功能:

  • 选择目标
  • 选择功能
  • 在后台模式中,勾选位置更新

关于ios - 使用 swift 制作一个获取用户位置的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749388/

相关文章:

ios - 在同一个 Xcode 项目中拥有 iOS 和 macOS 应用程序

xcode - 背景音乐不起作用,但没有错误?

ios - AFNetworking-发布json-> 400错误请求

javascript - 如何更改axios url?

ios - AVPlayerLoop 没有无缝循环 - Swift 4

php - 在mysql中通过lat lng获取10km以内的记录

android - 我如何制作一个 Gps 位置提供者? (USB 全局定位系统)

android - 某些设备上的位置读数错误/准确性不佳

ios - 处理 Firebase 查询中由于元素数量较多而导致的内存使用情况

ios - 创建 CocoaPod 时“静态库不支持 Swift”