第一步 自己注册一个应用,然后获取里面的 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. }
  1. 注意: 1ab37d5dsa3a3c 是你的 App Key

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

他的返回参数是

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

效果图

根据Mob官网的天气预报接口写了一个简单的demo的更多相关文章

  1. 只是一个用EF写的一个简单的分页方法而已

    只是一个用EF写的一个简单的分页方法而已 慢慢的写吧.比如,第一步,先把所有数据查询出来吧. //第一步. public IQueryable<UserInfo> LoadPagesFor ...

  2. 写了一个简单的CGI Server

    之前看过一些开源程序的源码,也略微知道些Apache的CGI处理程序架构,于是用了一周时间,用C写了一个简单的CGI Server,代码算上头文件,一共1200行左右,难度中等偏上,小伙伴可以仔细看看 ...

  3. 自己写的一个简单PHP采集器

    自己写的一个简单PHP采集器 <?php //**************************************************************** $url = &q ...

  4. 写了一个简单可用的IOC

    根据<架构探险从零开始写javaweb框架>内容写的一个简单的 IOC 学习记录    只说明了主要的类,从上到下执行的流程,需要分清主次,无法每个类都说明,只是把整个主线流程说清楚,避免 ...

  5. 写了一个简单的 Mybatis

    写了一个简单的 Mybatis,取名 SimpleMybatis . 具备增删改查的基本功能,后续还要添加剩下的基本数据类型和Java集合类型的处理. 脑图中有完整的源码和测试的地址 http://n ...

  6. vue3官网介绍,安装,创建一个vue实例

    前言:这一章主要是vue的介绍.安装.以及如何创建一个vue实例. 一.vue介绍 vue3中文官网:建议先自己看官网. https://v3.cn.vuejs.org/ vue是渐进式框架,渐进式指 ...

  7. 写了一个简单的NodeJS实现的进程间通信的例子

    1. cluster介绍 大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,有人开发了第三方的cluster,让node可以利用多核CPU ...

  8. 利用HttpClient写的一个简单页面获取

    之前就听说过利用网络爬虫来获取页面,感觉还挺有意思的,要是能进行一下偏好搜索岂不是可以满足一下窥探欲. 后来从一本书上看到用HttpClient来爬取页面,虽然也有源码,但是也没说用的HttpClie ...

  9. 今天写了一个简单的新浪新闻RSS操作类库

    今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些. 一年前我写过一个RSS阅读器,不过 ...

随机推荐

  1. 快速构建Windows 8风格应用6-GridView数据控件

    原文:快速构建Windows 8风格应用6-GridView数据控件 本篇博文主要介绍什么是GridView数据控件.如何构建常用的GridView数据呈现样式. 什么是GridView数据控件? G ...

  2. C#在outlook里创建一封邮件到草稿箱

    原文:C#在outlook里创建一封邮件到草稿箱 1.引用Microsoft.Office.Interop.Outlook.dll 2.  实现代码 public static int SendToD ...

  3. __declspec(novtable)keyword

    __declspec (novtable )keyword,表示这个类不生成虚函数表.可是继承类不影响(无论基类是否使用了keyword). 不使用此keyword.类在生成对象时构造函数和析构函数多 ...

  4. Swift辛格尔顿设计模式(SINGLETON)

    本文已更新为2.0语法,具体查看:一叶单例模式 一.意图 保证一个类公有一个实例.并提供一个訪问它的全局訪问点. 二.使用场景 1.使用场景 当类仅仅能有一个实例并且客户能够从一个众所周知的訪问点訪问 ...

  5. NHibernate 数据查询之QueryOver<T>

    NHibernate 数据查询之QueryOver<T>   一.限制运算符 Where:筛选序列中的项目WhereNot:反筛选序列中的项目 二.投影运算符 Select:创建部分序列的 ...

  6. Andy Williams 《Love Story》

    where do i beginto tell a story of how great a love can bethe sweet love story that is older than th ...

  7. {{angular.js 使用技巧}} - 实现计算列属性

    前端MV*框架现在有很多,其中某些框架有计算列(又叫监控属性),比如:微软推荐的 Knockout.js 和博客园司徒正美的 avalon.js 框架. 本人只使用过 Knockout.js,aval ...

  8. document对象属性documentMode与CompatMode

    DOCTYPE DOCTYPE全称Document Type Declaration(文档类型声明,缩写DTD) DTD的声明影响浏览器对于CSS代码及Javascript脚本的解析. 渲染模式 渲染 ...

  9. mac下安装配置java jdk,maven,resin

    mac jdk安装及环境变量配置 安装http://www.ifunmac.com/2013/04/mac-jdk-7/ jdk安装目录:/Library/Java/JavaVirtualMachin ...

  10. javascript-无间缝滚动,封装

    原生javascript-无间缝滚动,封装 目前支持的是竖向与横向滚动 http://lgyweb.com/marScroll/ 现在分析下无间缝实现的基本思路(竖向例子): HTML结构: 1 &l ...