【问题标题】:Swift: location not being updated when returned from backgroundSwift:从后台返回时未更新位置
【发布时间】: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


    【解决方案1】:

    applicationWillEnterForeground 中,代码正在创建一个从未显示的ViewController 的新本地实例,还没有创建locationManager,因此没有任何效果。

    它不是指已经存在并显示的ViewController 实例(并且具有最初启动的locationManager 实例)。

    相反,它应该获得对现有实例的引用。假设ViewController 是根视图控制器,你可以这样做:

    func applicationWillEnterForeground(application: UIApplication) {
    
        if let rvc = window?.rootViewController as? ViewController {
            rvc.locationManager.startUpdatingLocation()
        }
    
    }
    


    但是,让ViewController 类自己管理自己的行为可能是更好的做法。这样,应用程序委托就不必找到对视图控制器实例的引用,也不会直接访问视图控制器的内部状态,ViewController 变得更加独立。

    除了应用程序委托方法applicationWillEnterForeground,还可以使用UIApplicationWillEnterForegroundNotification 通知从任何地方监视这些事件。

    ViewController 中,您可以在(例如)viewWillAppearviewWillDisappear 中注册和取消注册通知。注册的时候,你指明事件调用哪个方法,一切都在ViewController里面处理(applicationWillEnterForeground里面的代码可以去掉)。

    override func viewWillAppear(animated: Bool) {
        NSNotificationCenter.defaultCenter().addObserver(
            self, 
            selector: "willEnterForegound", 
            name: UIApplicationWillEnterForegroundNotification, 
            object: nil)
    }
    
    override func viewWillDisappear(animated: Bool) {
        NSNotificationCenter.defaultCenter().removeObserver(
            self, 
            name: UIApplicationWillEnterForegroundNotification, 
            object: nil)
    }
    
    func willEnterForegound() {
        locationManager.startUpdatingLocation()
    }
    

    【讨论】:

    • 谢谢 Anna,说得非常有道理,不知道我怎么会错过它正在创建 ViewController 的新实例,而不是使用现有的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多