我有以下代码来获取位置更新(iOS 7):
import UIKit
import CoreLocation
class FirstViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func startTracking(sender : AnyObject) {
NSLog("Start tracking")
if (locationManager == nil) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.pausesLocationUpdatesAutomatically = false
}
locationManager.startUpdatingLocation()
}
@IBAction func stopTracking(sender : AnyObject) {
NSLog("Stop tracking")
stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
NSLog("Error" + error.description)
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
println("locations = \(locations)")
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Access: Restricted"
break
case CLAuthorizationStatus.Denied:
locationStatus = "Access: Denied"
break
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Access: NotDetermined"
shouldIAllow = true
break
default:
locationStatus = "Access: Allowed"
shouldIAllow = true
}
NSLog(locationStatus)
}
}
我只收到一项更新didUpdateLocations
: 打电话后startTracking
the didUpdateLocations
仅调用一次,5 秒后 GPS 指示消失。
一些细节:
- 应用程序被授权使用位置服务
- 应用程序在前台
- 有趣的是:如果我在
didUpdateLocations
它将被击中 4 - 5 次。
我在这里看到了类似问题的答案(例如在 Swift 中实现 CLLocationManagerDelegate 方法 https://stackoverflow.com/questions/24050633/implement-cllocationmanagerdelegate-methods-in-swift),但它对我来说仍然不起作用。
我究竟做错了什么?
Thanks!
Xcode 6 对定位服务做了很多补充。
1)您需要更改 info.plist 文件以包含字符串“NSLocationWhenInUseUsageDescription”键。其价值将是您的应用程序打开位置服务的原因:“使用位置服务跟踪您的跑步”。
2)使用“[self.locationManager requestWhenInUseAuthorization];”在您的代码中使弹出窗口与您的上述字符串一起出现。
更多信息请参见 WWDC 2014 视频。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)