• 天气预报demo (ShareREC 官网 MobAPI)


    第一步 自己注册一个应用,然后获取里面的 App Key,下载MobAPI SDK

    然后拖入 MobAPI.framework 和 MOBFoundation.framework 到你的项目中

    官网是 : http://www.mob.com

    第二步  导入libraries  文件

    第三步 :  新建一个 UIViewController 控制器  --- WeatherMainViewController

    然后在  AppDelegate.m 里面写入如下  要导入

    #import "WeatherMainViewController.h"

    #import <MobAPI/MobAPI.h>

     1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     2     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
     3     self.window.backgroundColor = [UIColor whiteColor];
     4     // 1. 首页
     5     WeatherMainViewController *weather = [[WeatherMainViewController alloc] init ];
     6     self.window.rootViewController = weather;
     7     [self.window makeKeyAndVisible];
     8     
     9     [MobAPI registerApp:@"1ab37d5dsa3a3c"];
    10     return YES;
    11 }
    注意: 1ab37d5dsa3a3c 是你的 App Key

       第四:根据api文档获取里面的字段和返回参数取值

       他的返回参数是

      1                             {
      2   "msg": "success",
      3   "result": [
      4     {
      5       "airCondition": "",
      6       "city": "北京",
      7       "coldIndex": "低发期",
      8       "updateTime": "20150908153820",
      9       "date": "2015-09-08",
     10       "distrct": "门头沟",
     11       "dressingIndex": "短袖类",
     12       "exerciseIndex": "适宜",
     13       "future": [
     14         {
     15           "date": "2015-09-09",
     16           "dayTime": "阵雨",
     17           "night": "",
     18           "temperature": "24°C/18°C",
     19           "week": "星期三",
     20           "wind": "无持续风向小于3级"
     21         },
     22         {
     23           "date": "2015-09-10",
     24           "dayTime": "阵雨",
     25           "night": "阵雨",
     26           "temperature": "22°C/15°C",
     27           "week": "星期四",
     28           "wind": "无持续风向小于3级"
     29         },
     30         {
     31           "date": "2015-09-11",
     32           "dayTime": "",
     33           "night": "",
     34           "temperature": "23°C/15°C",
     35           "week": "星期五",
     36           "wind": "北风3~4级无持续风向小于3级"
     37         },
     38         {
     39           "date": "2015-09-12",
     40           "dayTime": "",
     41           "night": "",
     42           "temperature": "26°C/13°C",
     43           "week": "星期六",
     44           "wind": "北风3~4级无持续风向小于3级"
     45         },
     46         {
     47           "date": "2015-09-13",
     48           "dayTime": "",
     49           "night": "",
     50           "temperature": "27°C/16°C",
     51           "week": "星期日",
     52           "wind": "无持续风向小于3级"
     53         },
     54         {
     55           "date": "2015-09-14",
     56           "dayTime": "",
     57           "night": "多云",
     58           "temperature": "27°C/16°C",
     59           "week": "星期一",
     60           "wind": "无持续风向小于3级"
     61         },
     62         {
     63           "date": "2015-09-15",
     64           "dayTime": "少云",
     65           "night": "",
     66           "temperature": "26°C/14°C",
     67           "week": "星期二",
     68           "wind": "南风3级南风2级"
     69         },
     70         {
     71           "date": "2015-09-16",
     72           "dayTime": "局部多云",
     73           "night": "少云",
     74           "temperature": "26°C/15°C",
     75           "week": "星期三",
     76           "wind": "南风3级南风2级"
     77         },
     78         {
     79           "date": "2015-09-17",
     80           "dayTime": "阴天",
     81           "night": "局部多云",
     82           "temperature": "26°C/15°C",
     83           "week": "星期四",
     84           "wind": "东南风2级"
     85         }
     86       ],
     87       "humidity": "湿度:46%",
     88       "province": "北京",
     89       "sunset": "18:37",
     90       "sunrise": "05:49",
     91       "temperature": "25℃",
     92       "time": "14:35",
     93       "washIndex": "不适宜",
     94       "weather": "多云",
     95       "week": "周二",
     96       "wind": "南风2级"
     97     }
     98   ],
     99   "retCode": "200"
    100 }

    最后:

      1 //
      2 //  WeatherMainViewController.m
      3 //  全球天气预报
      4 //
      5 //  Created by txbydev3 on 17/1/11.
      6 //  Copyright © 2017年 陈竹青. All rights reserved.
      7 //
      8 
      9 //屏幕的宽度
     10 #define TXBYApplicationW ([UIScreen mainScreen].applicationFrame.size.width)
     11 //屏幕的高度
     12 #define TXBYApplicationH ([UIScreen mainScreen].applicationFrame.size.height)
     13 
     14 #import "WeatherMainViewController.h"
     15 #import "WeatherViewController.h"
     16 #import "Weather.h"
     17 #import "FutureWeather.h"
     18 #import "MJExtension.h"
     19 #import <MobAPI/MobAPI.h>
     20 #import <MOBFoundation/MOBFoundation.h>
     21 
     22 
     23 @interface WeatherMainViewController ()<UITableViewDelegate,UITableViewDataSource>
     24 
     25 @property (nonatomic, strong) NSArray *resultArray;
     26 @property (nonatomic, strong) NSArray *futureArray;
     27 
     28 @property (nonatomic, strong)  Weather *weather;
     29 
     30 @property (nonatomic, strong) UITableView *tableView ;
     31 
     32 @property (weak, nonatomic)   UILabel *city;
     33 @property (weak, nonatomic)   UILabel *airCondition;
     34 @property (weak, nonatomic)   UILabel *temperature;
     35 @property (weak, nonatomic)   UILabel *weatherLab;
     36 
     37 @end
     38 
     39 
     40 @implementation WeatherMainViewController
     41 
     42 
     43 
     44 - (void)viewDidLoad {
     45     [super viewDidLoad];
     46     [self request ];
     47 }
     48 
     49 
     50 - (void)setHeadView {
     51     UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
     52     imageView.image = [UIImage imageNamed:@"background"];
     53     [self.view addSubview:imageView];
     54      // 城市
     55     UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake((TXBYApplicationW - 80)*0.5, 30, 80, 25)];
     56     cityLabel.textColor = [UIColor whiteColor];
     57     cityLabel.font = [UIFont systemFontOfSize:23];
     58     [imageView addSubview:cityLabel];
     59     self.city = cityLabel;
     60     
     61     // 空气指数
     62     UILabel *air = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(cityLabel.frame) + 10, 120, 25)];
     63     air.textColor = [UIColor whiteColor];
     64         air.font = [UIFont systemFontOfSize:20];
     65     air.text = @"空气指数";
     66     [imageView addSubview:air];
     67    
     68     UILabel *airCondition = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(air.frame), CGRectGetMaxY(cityLabel.frame) + 10, TXBYApplicationW - 155, 25)];
     69     airCondition.textColor = [UIColor whiteColor];
     70         airCondition.font = [UIFont systemFontOfSize:20];
     71     [imageView addSubview:airCondition];
     72     self.airCondition = airCondition;
     73     
     74 //    温度
     75      UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(air.frame) + 10, 80, 25)];
     76     temp.textColor = [UIColor whiteColor];
     77     temp.text  =@"温度";
     78     temp.font = [UIFont systemFontOfSize:20];
     79     [imageView addSubview:temp];
     80  
     81     
     82     UILabel *temperature = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(temp.frame) , CGRectGetMaxY(airCondition.frame) + 10, 80, 25)];
     83     temperature.textColor = [UIColor whiteColor];
     84     temperature.font = [UIFont systemFontOfSize:20];
     85     [imageView addSubview:temperature];
     86     self.temperature = temperature;
     87     
     88     // 天气
     89     UILabel *weatherLab = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(temp.frame) + 10, 80, 25)];
     90     weatherLab.textColor = [UIColor whiteColor];
     91     weatherLab.text  =@"天气";
     92     weatherLab.font = [UIFont systemFontOfSize:20];
     93     [imageView addSubview:weatherLab];
     94     
     95     UILabel *weather = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(weatherLab.frame), CGRectGetMaxY(temperature.frame) + 10, 150, 21)];
     96     weather.textColor = [UIColor whiteColor];
     97     weather.font = [UIFont systemFontOfSize:22];
     98 //    weather.textAlignment = NSTextAlignmentCenter;
     99     [imageView addSubview:weather];
    100     self.weatherLab = weather;
    101     
    102     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(weather.frame) + 50, TXBYApplicationW, TXBYApplicationH *0.5) style:UITableViewStylePlain];
    103     self.tableView.delegate = self;
    104     self.tableView.dataSource = self;
    105     self.tableView.backgroundColor = [UIColor clearColor];
    106     self.tableView.tableFooterView = [UIView new];
    107     [imageView addSubview:self.tableView];
    108  }
    109 
    110 
    111 -(void)request  {
    112     //1.确定请求路径
    113    NSString *urlStr = @"http://apicloud.mob.com/v1/weather/query?key=1ab5503423a3c&city=苏州&province=江苏";
    114     // 防止 string 类型 转 url 为空
    115     urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    116      NSURL *url = [NSURL URLWithString:urlStr];
    117     NSURLRequest *request = [NSURLRequest requestWithURL:url];
    118      //3.获得会话对象
    119     NSURLSession *session = [NSURLSession sharedSession];
    120     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    121          if (error == nil) {
    122             //6.解析服务器返回的数据
    123             //说明:(此处返回的数据是JSON格式的,因此使用NSJSONSerialization进行反序列化处理)
    124             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    125              NSLog(@"%@",dict);
    126              [self setHeadView];
    127              self.resultArray = [Weather mj_objectArrayWithKeyValuesArray:dict[@"result"]];
    128              for (Weather *weather in self.resultArray) {
    129                  self.city.text = weather.city;
    130                  self.temperature.text = weather.temperature;
    131                  self.airCondition.text =weather.airCondition;
    132                  self.weatherLab.text =weather.weather;
    133                  self.futureArray = weather.future;
    134              }
    135         }
    136     }];
    137     //5.执行任务
    138     [dataTask resume];
    139 }
    140 
    141 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    142     return self.futureArray.count;
    143 }
    144 
    145 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    146     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    147     if (cell == nil) {
    148         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    149         cell.selectionStyle = UITableViewCellSelectionStyleNone;
    150         cell.backgroundColor = [UIColor clearColor];
    151     }
    152     FutureWeather *weather = self.futureArray[indexPath.row];
    153     
    154     cell.textLabel.text = weather.week;
    155     cell.detailTextLabel.text = weather.temperature;
    156     return cell;
    157  }
    158 
    159 
    160 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    161     return 45;
    162 }
    163 
    164 - (void)didReceiveMemoryWarning {
    165     [super didReceiveMemoryWarning];
    166 }
    167 
    168 @end

    效果图

      

  • 相关阅读:
    Oracle函数应用与查询聚合统计
    Oracle子查询与分页查询
    DB2端口(转自百度文库http://wenku.baidu.com/view/47809b26aaea998fcc220e65.html)
    职场生涯
    git 管理多个私钥
    ubuntu 解压 windows 生成的 zip 文件乱码问题
    js实现类似于add(1)(2)(3)调用方式的方法
    webkit内核浏览器的CSS写法
    python 单例模式
    Javascript模块化编程:AMD规范及require.js用法【转】
  • 原文地址:https://www.cnblogs.com/chenzq12/p/6274204.html
Copyright © 2020-2023  润新知