我想试着做一个天气预报应用。但我如何获取天气信息并在我的应用程序中使用它们?
我听说过Google IPA,但是怎么用呢?
发布于 2011-04-29 01:54:08
首先选择最适合您的天气应用编程接口:https://stackoverflow.com/questions/507441/best-weather-apis
包括Google在内的大多数API都以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度。
发布于 2018-11-15 15:08:21
我使用- Swift,面向协议的编程(POP),可编码的,MVVM,OpenWeatherMap设计模式和单元测试创建了示例iOS天气应用程序。
https://github.com/deepakiosdev/WeatherApp/tree/master
也许这会对那些正在寻找所有这些东西的人有所帮助。
https://stackoverflow.com/questions/5822186
复制相似问题