3

天気予報アプリを作ってみたいのですが、天気情報を取得してアプリで使用するにはどうすればよいですか?

Google IPA って聞いたけど、どうやって使うの?

4

2 に答える 2

9

まず、目的に最適な天気 API を選択してください: 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 度です。

于 2011-04-28T17:54:08.377 に答える