我想尝试制作一个天气应用程序......但是我如何获取天气信息并在我的应用程序中使用它们?
我听说过 Google IPA,但如何使用它?
首先为您的目的选择最佳天气 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 度。
我使用 Swift、面向协议的编程 (POP)、Codable、OpenWeatherMap Api、带有单元测试的 MVVM 设计模式创建了示例iOS 天气应用程序。
https://github.com/deepakiosdev/WeatherApp/tree/master
可能对正在寻找所有这些东西的人有所帮助。