簡體   English   中英

使用Swift獲取用戶位置來制作應用

[英]Making an app using swift that gets user's location

我運行了獲取用戶位置的代碼,這是我得到的錯誤。 有人可以幫我解決嗎?

我的代碼如下:

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) {

}



}

打印到控制台的錯誤

聽起來您沒有啟用目標的位置更新功能:

  • 選擇目標
  • 選擇能力
  • 在后台模式下,勾選位置更新

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM