【发布时间】:2015-02-23 05:33:17
【问题描述】:
我有一个 Swift 应用程序,我试图在应用程序从后台返回时更新位置,但从后台返回时它似乎不起作用。
在启动应用程序时,我会很好地获取位置。获取位置后,我调用 stopUpdatingLocation() 所以我不会继续获取位置: locationManager.stopUpdatingLocation()
然后,在我的 AppDelegate.swift 中我再次 startUpdatingLocation:
func applicationWillEnterForeground(application: UIApplication) {
ViewController().locationManager.startUpdatingLocation()
}
这是我目前的代码:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error while updating location " + error.localizedDescription)
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var userLocation:CLLocation = locations[0] as CLLocation
println("\(userLocation.coordinate.latitude),\(userLocation.coordinate.longitude)")
locationManager.stopUpdatingLocation()
}
}
但是,每当我在应用程序后台运行(单击主页),然后返回应用程序时,位置都不会更新。知道我在这里可能做错了什么吗?
【问题讨论】:
标签: swift core-location