视图加载之前位置未更新 [英] Location not updating before the view loads

查看:86
本文介绍了视图加载之前位置未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量 zipCode,在将视图加载到scrollView之前,我试图将其更新为用户的当前位置。目前,一旦我选出并返回到scrollView视图控制器,它就会更新到正确的位置。另外,我应该在哪里实施更改以进行全新安装的授权更新。感谢您对高级的任何帮助。

I have a variable "zipCode" that I am trying to update to the users current location before the views load in my scrollView. Currently it updates to the correct location once I segue out and back into the the scrollView view controller. Also where I should implement the change for authorization update for a fresh install. Thank you for any help in advanced.

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))

    }
}


推荐答案

您注意到的原因是 CLLocationManager 是一个异步类,这意味着从启动它到收到位置之间可能会有延迟。此延迟可能并不总是很大,但在大多数情况下足以在 viewDidLoad 之后运行 didUpdateLocations

What you noticed is due to the fact that CLLocationManager is an asynchronous class, this meaning that there can be a delay between the moment you start it and the moment you receive the location. This delay might not always be big, but in most cases is enough to have didUpdateLocations run after viewDidLoad.

类似地,如果用户尚未授权您的应用程序进行位置使用,则只有在用户按下允许后,您才能获取位置(否则,您将收到失败的信息。 t批准)。

Similarly, if the user has not yet authorized your application for location usage, you'll only obtain the location after the user pressed allow (or you receive a failure if the user doesn't approve it).

一些建议:


  1. 准备接受显示视图后的位置,例如显示一些内容丰富的文本(或微调框)。或者,如果您尚未收到位置,请尽快要求位置,不要前进到有问题的控制器(以某种方式通知用户)

  2. 取决于规格在您的应用程序中,您可以仅在需要时(例如在代码中)请求位置授权,也可以在启动时

  3. 将与位置管理器相关的代码包装到您的自定义类中,您只需索要它的位置,当您需要在多个控制器中使用位置时,这将使事情变得更容易。

nshipster.com 上有关CoreLocation的非常有用的文章,我建议您仔细阅读,它会阐明很多方面关于iOS中的位置服务。您可以在此处

There's a very useful article on nshipster.com about CoreLocation, I'd recommend you go through it, it will clarify a lot of aspects about location services in iOS. You can find the article here

这篇关于视图加载之前位置未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆