IOS天气预报(美化)

接口调试 http://www.zdoz.net/interfaces.aspx#weat-link

气象天气来自中国天气网

http://www.weather.com.cn

工程地址: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];
    }
}




  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值