IOS天气预报(美化)

清雨小竹 发表于 2022/09/25 01:13:56 2022/09/25
【摘要】 接口调试 http://www.zdoz.net/interfaces.aspx#weat-link 气象天气来自中国天气网 http://www.weather.com.cn 工程地址:http://download.csdn.net/download/zzzili/5043081 天气预报城市代码 ...

接口调试 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




效果如图

:


  
  1. -(void)GetWeatherByCityId:(NSString*)cityId
  2. {
  3. NSURL *URL =[NSURL URLWithString:[NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/%@.html",cityId]];
  4. NSError *error;
  5. NSString *stringFromFileAtURL = [[NSString alloc]
  6. initWithContentsOfURL:URL
  7. encoding:NSUTF8StringEncoding
  8. error:&error];
  9. NSString *strTempL;
  10. NSString *strTempH;
  11. NSString *strWeather;
  12. if(stringFromFileAtURL !=nil)
  13. {
  14. NSLog(stringFromFileAtURL);
  15. NSArray *strarray = [stringFromFileAtURL componentsSeparatedByString:@"\""];
  16. for(int i=0;i<strarray.count;i++)
  17. {
  18. NSLog([strarray objectAtIndex:i]);
  19. NSString *str = [strarray objectAtIndex:i];
  20. if(YES == [str isEqualToString:@"temp1"])//最高温度
  21. {
  22. strTempH = [strarray objectAtIndex:i+2];
  23. }
  24. else if(YES == [str isEqualToString:@"temp2"])//最低温度
  25. {
  26. strTempL = [strarray objectAtIndex:i+2];
  27. }
  28. else if(YES == [str isEqualToString:@"weather"])//天气
  29. {
  30. strWeather = [strarray objectAtIndex:i+2];
  31. }
  32. }
  33. NSString *sweather = [[NSString alloc]initWithFormat:@"%@\n%@~%@",strWeather,strTempL,strTempH];
  34. if(sweather !=nil)
  35. self.labelWeather.text = sweather;
  36. NSRange range = [strWeather rangeOfString:@"转"];
  37. if(range.location != NSNotFound)
  38. {
  39. range.location += 1;
  40. range.length = strWeather.length - range.location;
  41. strWeather = [strWeather substringWithRange:range];
  42. }
  43. range = [strWeather rangeOfString:@"到"];
  44. if(range.location != NSNotFound)
  45. {
  46. range.location += 1;
  47. range.length = strWeather.length - range.location;
  48. strWeather = [strWeather substringWithRange:range];
  49. }
  50. //获取当前时间
  51. NSDate *date = [NSDate date];
  52. NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
  53. NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
  54. comps = [calendar components:NSHourCalendarUnit fromDate:date];
  55. int hour = comps.hour;
  56. NSString *fileName;
  57. if(hour>6&&hour<18)
  58. {
  59. fileName = [[NSString alloc ]initWithFormat:@"%@日.png",strWeather];
  60. }
  61. else
  62. {
  63. fileName = [[NSString alloc ]initWithFormat:@"%@夜.png",strWeather];
  64. }
  65. NSLog(fileName);
  66. self.imgWeather.image = [UIImage imageNamed:fileName];
  67. [fileName release];
  68. }
  69. }




文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/8558795

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。