Xcode天气应用程序

时间:2011-04-28 16:51:38

标签: xcode api google-api weather

我想尝试创建一个天气应用...但是如何获取天气信息并在我的应用中使用它们?

我听说过Google IPA但是如何使用它?

2 个答案:

答案 0 :(得分:7)

首先选择最适合您的天气API: https://stackoverflow.com/questions/507441/best-weather-apis

大多数API(包括Google)都会以XML格式返回结果 快速编写示例代码,帮助您开始使用Google Weather API:

NSString * location =  @"Amsterdam";
NSString * address = @"http://www.google.co.uk/ig/api?weather=";
NSString * request = [NSString stringWithFormat:@"%@%@",address,location];
NSURL * URL = [NSURL URLWithString:request];
NSError * error;    
NSString* XML = [NSString stringWithContentsOfURL:URL encoding:NSASCIIStringEncoding error:&error];

// Extract current temperature the 'dirty' way
NSString * temp = [[[[XML componentsSeparatedByString:@"temp_c data=\""] objectAtIndex:1] componentsSeparatedByString:@"\""] objectAtIndex:0];
NSLog(@"It's currently %@ degree in %@.", temp, location);

// Parse the XML with NSXMLDocument to extract the data properly
NSXMLDocument * doc = [[NSXMLDocument alloc] initWithXMLString:XML options:0 error:NULL];

输出:

  

目前在阿姆斯特丹有14度。

答案 1 :(得分:0)

我使用-Swift,面向Protocaol的编程(POP),Codable,OpenWeatherMap Api和带有单元测试的MVVM设计模式创建了示例 iOS Weather应用

https://github.com/deepakiosdev/WeatherApp/tree/master

也许对正在看所有这些东西的人有帮助。