开发者

Simple question from a novice iPhone programmer

I apologize开发者_如何学C for what I realize is likely a very simple question - I'm very new to programming and what is likely very simple for the majority of you, I've been struggling with for quite a while.

Essentially, I've been working on a simple weather app which fetches it's information from the Google weather API. The API is set up such that you simply append the town name or zip code to the end of the URL.

query = @"toronto,on";

CXMLDocument *parser = [[[CXMLDocument alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@", query]] options:0 error:nil] autorelease];

As you can see, by changing the value "query", I control what town or city the weather is being fetched for. The rest of the app works relatively well - the weather data is displayed and it is reliable for the most part. However, it only displays the weather for whatever city I code into the app, rendering it useless for anyone apart from the residents of my city.

My question is what would be the easiest way to first prompt the user to type in their location, then take that location and have it equal the "query" string.

As I said, this is likely a very simple question, however being new to programming in general I've been struggling with it for quite a while. I'd be perfectly happy give a few bucks via PayPal to a response which answers my question totally.

If you need any more information to better answer the question please don't hesitate to ask, I'm happy to provide it.

Thanks again!


I would suggest using Location Services, so your user does not need to type his/her location at all. Have a look here: Understanding Location Services.

In this S.O. article, you will find a singleton class which should give you your current location. In order to use this class, I would do like this in your main controller (or when you want to update your location):

 ...
 CLLocation *myLocation = [[LocationManager sharedInstance] currentLocation];
 if ([[LocationManager sharedInstance] locationKnown]) {
      [self updateWeatherInfo];
 } else {
      [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkLocation:) userInfo:nil repeats:YES];
 }
 ...

Provide an implementation for checkLocation:

- (void)checkLocation:(NSTimer*)theTimer {
     if ([[LocationManager sharedInstance] locationKnown) {
          [theTimer invalidate];
          [self updateWeatherInfo];
     }
}

Where updateWeatherInfo is a method provided by you to retrieve the new weather forecast from the server. In that method you would convert your currentLocation into a city name by using the code in the sample below.

This is a full sample from Apple showing how to get your current address (country, city, or street) from your location.

Pretty much all you need, I think.


Easiest way would be to prompt the user for their 6-digit zip code in a UITextField, and use that text in your query. Just make sure it is 6 digits and only numbers beforehand.

If you are struggling with something like this, I highly recommend you watch the Stanford iOS course available on iTunes U. The first few courses will show you much of what you might want to develop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜