在 Swift 中实现 CLLocationManagerDelegate 方法
-
21-12-2019 - |
题
我已经尝试让它工作一段时间了,我来这里是想问 - 我如何在 Swift 中使用 CLLocationManagerDelegate 方法?我把这个放在班级的前列:
var locationManager = CLLocationManager()
我已将以下内容放入我的 viewDidLoad
方法:
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
我尝试使用这些委托方法但无济于事:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
locationReceived = true
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationReceived = false
}
我也尝试过在函数前面使用 @Optional,但 Xcode 会抛出编译器错误。有任何想法吗?
解决方案
您需要添加 NSLocationAlwaysUsageDescription
或者 NSLocationWhenInUseUsageDescription
如果您还没有,它们现在是强制性的,
iOS8+ 需要将这两个字符串之一设置为使用位置。您使用哪一种取决于您想要如何询问位置。
使用
NSLocationAlwaysUsageDescription
适用于想要使用设备位置的应用程序,即使该应用程序未打开且正在使用。使用
NSLocationWhenInUseUsageDescription
适用于仅在应用程序打开并使用时才希望使用设备位置的应用程序。
笔记: 添加字符串后,在构建和运行之前,从设备中删除该应用程序并让它进行全新安装。看起来,如果应用程序在您升级到 iOS8 之前被授权使用位置,它不会再次请求您的许可,也不会看到您设置了这些字符串。执行删除并全新安装可以解决此问题。
设置任一字符串都会在安装/首次使用时弹出如下提示:“即使您没有使用该应用程序,也允许“此应用程序”访问您的位置”
这是一个 截屏 的 列表 文件。
其他提示
首先在plist文件中添加这两行
1)nslocationWheniNuseUseSusageScription
2)NSLocationalWaysUsAgentescription
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
}
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.locationServicesEnabled
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if (error) {
if (seenError == false) {
seenError = true
print(error)
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
println(coord.latitude)
println(coord.longitude)
}
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
. 获取用户当前位置:-
步骤1: let locationManager = CLLocationManager() // make object of CLLocationManager class.
第2步: In viewDidLoad instantiate the CLLocationManager class like,
// 用于后台
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.locationServicesEnabled())
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
步骤3: 现在实现委托方法 CLLocationManager
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
println("locations = \(locValue.latitude) \(locValue.longitude)")
}
步骤4:不要忘记添加 NSLocationAlwaysUsageDescription
在 iOS 8 中的 Info.plist 中,必须添加此内容。这将请求使用用户位置的许可。
我有同样的问题。Didupdatelocations - 没有工作。运行您的应用程序。 转到设置页面 - >隐私 - >位置并关闭位置服务。DidfailWithError将捕获错误的位置服务。然后打开它。自那一刻以来,迪普特替补产后将捕获位置。
这是一个旧线程的一点,但上面都没有在swift 2.2 xcode 7.3.1上为我工作。
问题是我使用的:
func locationManager(manager: CLLocationManager!, didUpdateLocation locations: [AnyObject]!) {
print("Got location")
locationManager.stopUpdatingLocation()
}
.
,这从未被称为。当我更改为:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Got location")
locationManager.stopUpdatingLocation()
}
.
全部制定了。似乎在使用[AnyObject]时未调用代表?