IOS天气预报(美化)

简介: IOS天气预报(美化)

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


20130610230951421.png


气象天气来自中国天气网


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


20130607103932281.png


效果如图

1359601427_7913.png

-(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];
    }
}
相关文章
|
iOS开发
【iOS】自定义表情的实现
现在很多App都有自己的自定义的表情,客户端可能要实现自己定表情的选择、发送和显示,这里我介绍一种简单的自定义表情的实现思路和代码。 以下介绍基本可以实现聊天中对自定义表情的需求,可结合下图来理解: 资源准备: 自定义表情图片的命名。
1305 0
|
定位技术 C++ iOS开发
|
定位技术 iOS开发 程序员
|
iOS开发 API 开发者
iOS系统预览文件但不分享的实用技巧 (iOS10, QLPreviewController)
苹果系统的文件预览能力对于很多app来说是必要的,但其提供的UI相关api并不是可以定制的,比如需要定制toolbar, navigationbar的情况,直接使用QLPreviewController或者 UIDocumentInteractionController 并不能达到想要的效果。
2446 0
|
定位技术 iOS开发 开发工具