快速的位置

时间:2015-07-18 16:11:39

标签: swift

我想在用户再次进入应用时更新位置。如果用户打开应用程序,您将获得正确的数据,但是当您关闭应用程序(主页按钮)并再次打开它时,它会进入刷新功能,但不会进入位置功能。我无法得到它。这是我的代码:

import UIKit
import CoreLocation

var request : NSMutableURLRequest = NSMutableURLRequest()


class ViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
         super.viewDidLoad()

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

    func refresh(){
        println("update")
        locationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
    {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in

    if (error != nil)
    {
        println("Error: " + error.localizedDescription)
    return
    }

    if (placemarks.count > 0)
    {
        let pm = placemarks[0] as! CLPlacemark
        self.displayLocationInfo(pm)

    }
    else
    {
        println("Error with the data.")
    }
    })
}

在我的app委托中我有这个:

 func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

            ViewController().refresh();

    }

所以它进入了刷新功能,但它注意到了..它可以是什么? 谢谢!

2 个答案:

答案 0 :(得分:1)

applicationWillEnterForeground(application: UIApplication)的每次调用中,您创建了ViewController的新对象,在其上调用refresh()并让它消失。这不是呈现给用户的视图控制器对象 相反,您必须获取呈现的ViewController对象并在其上调用refresh()

可能类似于

func applicationWillEnterForeground(application: UIApplication) {
        let viewController=  self.window.rootViewController as! ViewController
        viewController.refresh()
}

但它看起来如何,取决于我们不知道的代码中的细节。

或完全删除该行并在ViewController的viewDidLoad()

中订阅WillEnterForeground通知
override func viewDidLoad()
{
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationWillEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
}

func applicationWillEnterForeground(notification: NSNotification) {
    self.refresh()
}


deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

答案 1 :(得分:0)

您遇到的挑战是当您按下主页时,您的视图不会卸载,而只是暂停。因此,当您的应用程序恢复时,不会调用viewDidLoad方法...

您实际在做的是处理进入前台的应用程序,您必须注册才能接收有关的通知。将以下行添加到viewDidLoad方法:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("applicationWillEnterForeground"), name: UIApplicationWillEnterForegroundNotification, object: nil)

这将为UIApplicationWillEnterForegroundNotification事件添加一个观察者,然后调用方法applicationWillEnterForeground。现在您需要做的就是创建方法applicationWillEnterForeground并从中调用您的refresh方法。