视图加载前位置未更新

时间:2016-01-04 06:39:41

标签: ios swift core-location

我有一个变量" zipCode"我试图在我的scrollView加载视图之前更新到用户的当前位置。目前,一旦我离开并返回到scrollView视图控制器,它将更新到正确的位置。此外,我应该为全新安装实现授权更新的更改。感谢您提供任何帮助。

import UIKit
import CoreLocation

var zipCode = 90210
var location = [""]

class ViewController: UIViewController, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()

    @IBOutlet var scrollView: UIScrollView!

    override func viewDidLoad() {
            super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
    }

    ////Current Location
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error)-> Void in
            if (error != nil) {
                print("Error: + error.localizedDescription")
                return
            }
            if placemarks!.count > 0 {
                let pm = placemarks![0]
                self.displayLocationInfo(pm)
            }else {
                print("Error with data")
            }
        })
    }

    func displayLocationInfo(placemark: CLPlacemark) {
            self.locationManager.stopUpdatingLocation()
            let zip = placemark.postalCode
            if let zip2 = numberFormatter.numberFromString(zip!)?.integerValue {
                zipCode = zip2
            }
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error: " + error.localizedDescription)
    }

    ///// Load Views
    override func viewWillAppear(animated: Bool) {
        let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard0") as UIViewController!
        self.addChildViewController(vc0)
        self.scrollView.addSubview(vc0.view)
        vc0.didMoveToParentViewController(self)
        vc0.view.frame = scrollView.bounds
        vc0.viewDidLoad()

        let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard2") as UIViewController!
        self.addChildViewController(vc1)
        self.scrollView.addSubview(vc1.view)
        vc1.didMoveToParentViewController(self)
        vc1.view.frame = scrollView.bounds
        var vc1Frame: CGRect = vc1.view.frame
        vc1Frame.origin.x = self.view.frame.width
        vc1.view.frame = vc1Frame
        vc1.viewDidLoad()

        let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard1") as UIViewController!
        self.addChildViewController(vc2)
        self.scrollView.addSubview(vc2.view)
        vc2.didMoveToParentViewController(self)
        vc2.view.frame = scrollView.bounds
        var vc2Frame: CGRect = vc2.view.frame
        vc2Frame.origin.x = 2 * self.view.frame.width
        vc2.view.frame = vc2Frame
        vc2.viewDidLoad()

        let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("storyBoard3") as UIViewController!
        self.addChildViewController(vc3)
        self.scrollView.addSubview(vc3.view)
        vc3.didMoveToParentViewController(self)
        vc3.view.frame = scrollView.bounds
        var vc3Frame: CGRect = vc3.view.frame
        vc3Frame.origin.x = 3 * self.view.frame.width
        vc3.view.frame = vc3Frame
        vc3.viewDidLoad()

        self.scrollView.contentSize = CGSizeMake((self.view.frame.width) * 4, (self.view.frame.height))

    }
}

1 个答案:

答案 0 :(得分:1)

您注意到的原因是CLLocationManager是一个异步类,这意味着在您启动它的那一刻到您收到该位置的那一刻之间可能存在延迟。这种延迟可能并不总是很大,但在大多数情况下足以让didUpdateLocationsviewDidLoad之后运行。

同样,如果用户尚未授权您的应用程序使用位置,您只能在用户按下允许后获取该位置(或者如果用户不批准则会收到失败)。

一些建议:

  1. 准备在视图显示后接收位置,例如显示一些信息性文本(或微调)。或者尽快询问位置,如果您还没有收到位置,请不要向前移动到有问题的控制器(以某种方式通知用户)
  2. 根据您的应用程序的规格,您可以仅在需要时(例如在您的代码中)或在启动时请求位置授权
  3. 将位置管理器相关代码打包到您的自定义类中,您可以简单地询问它的位置,当您需要在多个控制器中使用位置时,这将使事情变得更容易。
  4. 关于CoreLocation的nshipster.com上有一篇非常有用的文章,我建议你仔细阅读它,它将阐明有关iOS中位置服务的很多方面。您可以找到文章here