新手 iPhone 程序员的简单问题

发布于 2024-11-19 12:05:50 字数 743 浏览 6 评论 0原文

我为我意识到可能是一个非常简单的问题而道歉 - 我对编程非常陌生,对于你们大多数人来说可能非常简单,我已经挣扎了很长一段时间。

本质上,我一直在开发一个简单的天气应用程序,它从 Google 天气 API 获取信息。 API 的设置使您只需将城镇名称或邮政编码附加到 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];

正如您所看到的,通过更改“查询”值,我可以控制获取天气的城镇或城市。该应用程序的其余部分运行得相对良好 - 显示天气数据并且在很大程度上是可靠的。然而,它只显示我在应用程序中编码的任何城市的天气,这使得除了我所在城市的居民之外的任何人都无法使用它。

我的问题是,首先提示用户输入其位置,然后获取该位置并使其等于“查询”字符串的最简单方法是什么。

正如我所说,这可能是一个非常简单的问题,但是作为一般编程新手,我已经在这个问题上挣扎了很长一段时间。我非常乐意通过 PayPal 给一些完全回答我的问题的答复。

如果您需要更多信息来更好地回答问题,请随时询问,我很乐意提供。

再次感谢!

I apologize 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-11-26 12:05:50

我建议使用位置服务,这样您的用户根本不需要输入他/她的位置。请查看此处:了解位置服务

在这篇SO文章中,您将找到一个单例类,它应该为您提供当前位置。为了使用这个类,我会在你的主控制器中这样做(或者当你想更新你的位置时):

 ...
 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];
 }
 ...

提供 checkLocation 的实现:

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

其中 updateWeatherInfo 是您提供的从服务器检索新天气预报的方法。在该方法中,您可以使用下面示例中的代码将 currentLocation 转换为城市名称。

这是 Apple 提供的完整示例,展示了如何从您所在的位置获取您当前的地址(国家/地区、城市或街道)。

我想这几乎是你所需要的一切。

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.

唯憾梦倾城 2024-11-26 12:05:50

最简单的方法是提示用户在 UITextField 中输入 6 位邮政编码,并在查询中使用该文本。只要事先确保它是 6 位数字且仅是数字即可。

如果您正在为此类问题苦苦挣扎,我强烈建议您观看 iTunes U 上提供的斯坦福 iOS 课程。前几门课程将向您展示您可能想要开发的大部分内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文