开发者

Xcode Weather app

开发者 https://www.devze.com 2023-03-01 03:55 出处:网络
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 ab开发者_运维技巧out Google IPAbut how to use it?First pick the best weather API for your purp

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 ab开发者_运维技巧out Google IPA but how to use it?


First pick the best weather API for your purpose: 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:

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.


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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消