-
請問有其他的API可以替代嗎?
感謝
- #2 shenfive 於 2016/06/17 14:16
-
http://opendata.cwb.gov.tw/
首先看一下 YaHoo Weateher API http://developer.yahoo.com/weather/
簡單的說就是YaHoo 提供了一個天氣的 RSS 與相關的資料。
如取得台北的天氣就可以由 http://weather.yahooapis.com/forecastrss?w=2306179
上面的 w= 就是寫入WOEID 由 Yahoo 所要求的位置ID, 12, 或者,也可以加上 &u=c
woeid 可以由 http://weather.yahoo.com/ 的查詢網址來看,或參考熱心網友的提供
http://ifansland.com/thread-15103-1-1.html
得到相關的RSS資料,再自行 做 XML Parser
首先在要查的程式中,加入
NSURL *yahooWetherTaipeiURL=[NSURLURLWithString:@"http://weather.yahooapis.com/forecastrss?w=12703515"];
NSData *theData=[NSData dataWithContentsOfURL:yahooWetherTaipeiURL];
NSXMLParser *theXMLParser=[[NSXMLParseralloc] initWithData:theData];
[theXMLParser setDelegate:self];
[theXMLParser parse];
請記得我們設定了 self 為 delegate ,所以要在 .h 檔宣告
<NSXMLParserDelegate>
另外,在 self 中,加入以下的代理 method 就會輸出現在的天氣了
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"yweather:condition"]) {
NSLog(@"Today's weather is %@",[attributeDict objectForKey:@"text"]);
}
}
輸出如下
2012-12-18 16:00:10.056 JPTest[8697:c07] Today's weather is Light Rain
留言列表