iOS多线程之简单天气预报应用

做了个天气预报的小demo,主要使用了serchbar和imageView、textfiled控件,以及http协议、Json解析。

在这里有一点小小的建议,CSDN的源代码语言难道不能提供更多的选择吗??只用那么少的几种,C和OC都没有。


//
//  KevinViewController.m
//  weatherTest


#import "KevinViewController.h"

@interface KevinViewController ()
@property (strong, nonatomic) IBOutlet UISearchBar *searchBarView;
@property (strong, nonatomic) IBOutlet UITextView *weatherDisplay;
@property (strong, nonatomic) IBOutlet UIImageView *weatherImage;
- (IBAction)tapGester:(UITapGestureRecognizer *)sender;


@end

@implementation KevinViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.searchBarView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
}



#pragma mark searchBar

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
  [searchBar endEditing:YES];
  NSString *urlString2 = [NSString stringWithFormat:@"http://weather.china.xappengine.com/api?city=%@",self.searchBarView.text];
  urlString2 = [urlString2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSURL *url = [NSURL URLWithString:urlString2];
  
  //打开网站,使用内置safari
 
  NSMutableString *resultString = [NSMutableString stringWithString:@"city:\n"];
  [resultString appendFormat:@"%@\n",self.searchBarView.text];
  NSLog(@"resultString :%@",resultString);
  
  
  NSOperationQueue *download = [[NSOperationQueue alloc]init];
  [download addOperationWithBlock:^(void){
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error = nil;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
   
    NSString *pub = result[@"pub"];
    [resultString appendFormat:@"publish:\n%@\n",pub];

    /*json解析,是数组和字典
     {"pub" : "2013-05-03 07:58",
     "name" : "成都",
     "wind" : {"chill" : 61, "direction" : 290, "speed" : 2},
     "astronomy" : {"sunrise" : "6:18", "sunset": "19:42"}, 
     "atmosphere" : {"humidity" : 93, "visibility" : 3.73, "pressure" : null, "rising": 0},
     "forecasts" : [{"date" : "2013-05-03", "day" : 5, "code" : 26, "text" : "多云", "low" : 19, "high" : 28, "image_large" : "http://weather.china.xappengine.com/static/w/img/d26.png", "image_small" : "http://weather.china.xappengine.com/static/w/img/s26.png"}, {"date" : "2013-05-04", "day" : 6, "code" : 38, "text" : "零星雷雨", "low" : 18, "high" : 24, "image_large" : "http://weather.china.xappengine.com/static/w/img/d38.png", "image_small" : "http://weather.china.xappengine.com/static/w/img/s38.png"}]}*/
    
    //取风力等级
    NSDictionary *wind = result[@"wind"];
    [resultString appendFormat:@"wind:\n%@\n",wind[@"speed"]];
    //self.wind.text = temp;
    
    //取天气情况
    NSArray *forecasts = result[@"forecasts"];
    NSDictionary *date = forecasts[0];
    [resultString appendFormat:@"weather:\n%@\n",date[@"text"]];
    //self.weather.text = date[@"text"];
    
    
    NSString *image_large = date[@"image_large"];
    NSData *imageWeatherData = [NSData dataWithContentsOfURL:[NSURL URLWithString:image_large]];
    UIImage *imageWeather =[UIImage imageWithData:imageWeatherData];
    
    
    //主队列处理ui
    [[NSOperationQueue mainQueue]addOperationWithBlock:^(void){
      self.weatherDisplay.text = resultString;
      self.weatherImage.image = imageWeather;
      
    }];
  }];
  
}

#pragma mark 结束编辑,收回键盘
- (IBAction)tapGester:(UITapGestureRecognizer *)sender
{
  [self.view endEditing:YES];

}
@end


运行如下图;

iOS多线程之简单天气预报应用_第1张图片


你可能感兴趣的:(iOS多线程之简单天气预报应用)