gpt4 book ai didi

ios - didUpdateLocations 从未调用过

转载 作者:IT王子 更新时间:2023-10-29 05:39:53 31 4
gpt4 key购买 nike

我正在尝试获取用户的位置。为此,我在 info.plist 中设置了以下属性:

enter image description here

我还在 viewDidLoad 方法和下面的函数中添加了以下代码。问题是 locationManager(manager, didUpdate....) 函数永远不会被调用,我也永远不会收到访问位置权限的提示,即使我已经删除并重新安装了该应用程序。我在我的 iPad 上测试这个,而不是在模拟器上。 didFailWithError 函数也永远不会被调用。

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



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("UPDATING")
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
let latitude = locValue.latitude
let longitude = locValue.longitude
latitudeText = "\(latitude)"
longitudeText = "\(longitude)"
if let a = latitudeText, b = longitudeText {
print(a)
print(b)
self.locationManager.stopUpdatingLocation()
if (userAlreadyExist()) {
dispatch_async(dispatch_get_main_queue(), {
//self.performSegueWithIdentifier("segueWhenLoggedIn", sender: self)
self.performSegueWithIdentifier("showCamera", sender: self)
// self.performSegueWithIdentifier("showTabBarController", sender: self)
})

}
else {
dispatch_async(dispatch_get_main_queue(), {
self.performSegueWithIdentifier("segueWhenLoggedOut", sender: self)
})
}
}


}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error.localizedDescription)
}

编辑:

我添加了以下代码片段:

if CLLocationManager.locationServicesEnabled() {
print("yes")
}
else {
print("no")
}

它返回是。我还检查了我的设备,位置服务已启用,该应用程序列在那里,但是所有其他应用程序旁边都写有“使用时”、“从不”或“总是”,我的没有写任何东西。

最佳答案

从哪里开始位置更新?例如:

//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
_locationManager.allowsBackgroundLocationUpdates = true // allow in background

return _locationManager
}()


if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation() // start location manager

}

这是一个有效的 Controller 代码:对于设置自定义 iOS 目标属性也很重要。

将这两行添加到 Info.plist 中:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

//
// ViewController.swift
// LocationTest2


import UIKit
import CoreLocation

class ViewController: UIViewController {

//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
// _locationManager.allowsBackgroundLocationUpdates = true // allow in background

return _locationManager
}()

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

//allow location use
locationManager.requestAlwaysAuthorization()

print("did load")
print(locationManager)

//get current user location for startup
// if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
// }
}
}


// MARK: - CLLocationManagerDelegate
extension ViewController: CLLocationManagerDelegate {

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

for location in locations {

print("**********************")
print("Long \(location.coordinate.longitude)")
print("Lati \(location.coordinate.latitude)")
print("Alt \(location.altitude)")
print("Sped \(location.speed)")
print("Accu \(location.horizontalAccuracy)")

print("**********************")


}
}

}

关于ios - didUpdateLocations 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37683139/

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