3

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?

jonsca
  • 10,218
  • 26
  • 54
  • 62
Askapps
  • 69
  • 2
  • 12
  • You might find it useful https://github.com/hadanischal/WeatherRxSwift , https://github.com/hadanischal/WeatherRxSwift – Nischal Hada Aug 07 '19 at 09:45

2 Answers2

9

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.

Community
  • 1
  • 1
Anne
  • 26,765
  • 9
  • 65
  • 71
  • NICE:) but... can i just change the location "Amsterdam"? e.g. insted of NSString * location = @"Amsterdam" then NSString * location = @"Copenhagen" or NSString * location = @"London"???? – Askapps Apr 28 '11 at 17:58
  • 1
    Yes, simply change the `location` to request the information for another city or country. If the request is invalid, Goolge returns something like this within the XML: ``. Note: Implement `NSXMLDocument` to extract all data and handle errors properly. – Anne Apr 28 '11 at 18:01
  • 1
    It doesn't work for me, when I run the app it saids "It's currently (null) degree in Amsterdam." Please help :( –  Dec 03 '12 at 04:35
  • There is an error on "NSXMLDocument" so I used "NSXMLParser" instead, is this why I am getting this problem? I really need this :( –  Dec 03 '12 at 04:37
  • This is because Google shut down its Weather service – David Gölzhäuser Apr 22 '13 at 15:57
0

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.

Dipak
  • 2,263
  • 1
  • 21
  • 27