Xcode 天气应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5822186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:56:11  来源:igfitidea点击:

Xcode Weather app

xcodeapigoogle-apiweather

提问by Askapps

I want to try to make a weather app... but how do i get the weather info and use them in my app?

我想尝试制作一个天气应用程序……但是我如何获取天气信息并在我的应用程序中使用它们?

I heard about Google IPA but how to use it?

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

回答by Anne

First pick the best weather API for your purpose: https://stackoverflow.com/questions/507441/best-weather-apis

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

Most APIs, including Google, return their result in XML format.
Quickly written example code to get you started with the Google Weather API:

大多数 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];

Output:

输出:

It's currently 14 degree in Amsterdam.

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

回答by Deepak

I created sample iOS Weather appusing- Swift,Protocaol oriented programming(POP), Codable, OpenWeatherMap Api, MVVM design pattern with Unit Test.

我使用 Swift、面向协议的编程(POP)、Codable、OpenWeatherMap Api、带有单元测试的 MVVM 设计模式创建了示例iOS 天气应用程序

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

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

May be it will helpful for someone who is looking all these things.

可能对正在寻找所有这些东西的人有帮助。