接口调试 http://www.zdoz.net/interfaces.aspx#weat-link
气象天气来自中国天气网
工程地址:http://download.csdn.net/download/zzzili/5043081
天气预报城市代码
http://blog.csdn.net/zzzili/article/details/9045205
图标资源
http://download.csdn.net/detail/zzzili/5536725
效果如图
:
-(void)GetWeatherByCityId:(NSString*)cityId
{
NSURL *URL =[NSURL URLWithString:[NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/%@.html",cityId]];
NSError *error;
NSString *stringFromFileAtURL = [[NSString alloc]
initWithContentsOfURL:URL
encoding:NSUTF8StringEncoding
error:&error];
NSString *strTempL;
NSString *strTempH;
NSString *strWeather;
if(stringFromFileAtURL !=nil)
{
NSLog(stringFromFileAtURL);
NSArray *strarray = [stringFromFileAtURL componentsSeparatedByString:@"\""];
for(int i=0;i<strarray.count;i++)
{
NSLog([strarray objectAtIndex:i]);
NSString *str = [strarray objectAtIndex:i];
if(YES == [str isEqualToString:@"temp1"])//最高温度
{
strTempH = [strarray objectAtIndex:i+2];
}
else if(YES == [str isEqualToString:@"temp2"])//最低温度
{
strTempL = [strarray objectAtIndex:i+2];
}
else if(YES == [str isEqualToString:@"weather"])//天气
{
strWeather = [strarray objectAtIndex:i+2];
}
}
NSString *sweather = [[NSString alloc]initWithFormat:@"%@\n%@~%@",strWeather,strTempL,strTempH];
if(sweather !=nil)
self.labelWeather.text = sweather;
NSRange range = [strWeather rangeOfString:@"转"];
if(range.location != NSNotFound)
{
range.location += 1;
range.length = strWeather.length - range.location;
strWeather = [strWeather substringWithRange:range];
}
range = [strWeather rangeOfString:@"到"];
if(range.location != NSNotFound)
{
range.location += 1;
range.length = strWeather.length - range.location;
strWeather = [strWeather substringWithRange:range];
}
//获取当前时间
NSDate *date = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
comps = [calendar components:NSHourCalendarUnit fromDate:date];
int hour = comps.hour;
NSString *fileName;
if(hour>6&&hour<18)
{
fileName = [[NSString alloc ]initWithFormat:@"%@日.png",strWeather];
}
else
{
fileName = [[NSString alloc ]initWithFormat:@"%@夜.png",strWeather];
}
NSLog(fileName);
self.imgWeather.image = [UIImage imageNamed:fileName];
[fileName release];
}
}