dan
dan

Reputation: 729

xcode geolocation: Getting coordinates before view is loaded

I am trying to get the current location for the phone, and then send the coordinates to my webserver to process the data to return some results into my app.

The problem I am facing now is that the view loads first, then only it obtains the coordinates.

How do I get the coordinates first, so that I can send them to my webserver to process and send back the data so that I can be displayed on my view?

Thanks!

Upvotes: 1

Views: 1283

Answers (2)

Rob
Rob

Reputation: 437492

If your view controller really needs the location information before it is presented, it can have the presenting view controller perform the location services before the transition. If there is no presenting view controller (i.e. the view controller that needs the location is currently the root view controller), you can introduce a new root view controller which will perform the location services before transitioning to the main view controller.

This presenting view controller should (per the Location Awareness Programming Guide) should create the CLLocationManager, initiate location services, and upon receiving the appropriate didUpdateLocations of the CLLocationManagerDelegate, it can then stop location services and transition to the main view controller. Perhaps this intermediate view controller should also show some nice "please wait" screen or a copy of the default.png image with a UIActivityIndicatorView while the location services are being performed.

In practice, though, you don't necessarily need to introduce this intermediate view controller. You could do it all with your primary view controller. You can employ asynchronous event-driven coding patterns. So, rather than requiring the location before the view is presented, you can defer the performance of the web services until after the location services have been satisfied. For example, this simple test project invokes location services, and upon completion, submits a web request to a weather service, and parses the JSON response. No intermediate view controller was needed.

Note, though, that Apple frowns upon (i.e. may well reject) apps that only work if the location services succeed, so you may want to think about what your app is supposed to do if it simply is unable to use location services. You must gracefully handle that scenario, perhaps use the last known location.

Upvotes: 1

ganesh manoj
ganesh manoj

Reputation: 977

Then why cant you find current location in before view so that while pushing to that map location we can pass these coordinates and call web service so that we can display data i think then we will get what you want

Upvotes: 0

Related Questions