gpt4 book ai didi

ios - 使用 Core Location 的 Swift 后台获取位置更新

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:22 24 4
gpt4 key购买 nike

我想在我的应用程序上使用后台获取位置更新服务。但是不要在这里显示我的代码的任何输出我需要你的帮助。

import CoreLocation

class ViewController: UIViewController, UITextFieldDelegate, CLLocationManagerDelegate {

var locationManager:CLLocationManager = CLLocationManager()



override func viewDidLoad() {
super.viewDidLoad()


self.locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()


}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("didChangeAuthorizationStatus")

switch status {
case .NotDetermined:
print(".NotDetermined")
break

case .Authorized:
print(".Authorized")
self.locationManager.startUpdatingLocation()
break

case .Denied:
print(".Denied")
break

default:
print("Unhandled authorization status")
break

}
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations.last as CLLocation!

print("didUpdateLocations: \(location.coordinate.latitude), \(location.coordinate.longitude)")


}

仅提供输出

didChangeAuthorizationStatus
.NotDetermined

如果可能的话,我想在新位置发生变化时获取长纬度值。谢谢!

最佳答案

我将此添加到 info.plist 文件中:

<key>NSLocationAlwaysUsageDescription</key>
<string>Your message goes here</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message goes here</string>

关于ios - 使用 Core Location 的 Swift 后台获取位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35381375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com