根据Mob官网的天气预报接口写了一个简单的demo
第一步 自己注册一个应用,然后获取里面的 App Key,下载MobAPI SDK
然后拖入 MobAPI.framework 和 MOBFoundation.framework 到你的项目中
官网是 :http://www.mob.com
第二步 导入libraries 文件
第三步 : 新建一个 UIViewController 控制器 --- WeatherMainViewController
然后在 AppDelegate.m 里面写入如下 要导入
#import "WeatherMainViewController.h"
#import <MobAPI/MobAPI.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
// 1. 首页
WeatherMainViewController *weather = [[WeatherMainViewController alloc] init ];
self.window.rootViewController = weather;
[self.window makeKeyAndVisible]; [MobAPI registerApp:@"1ab37d5dsa3a3c"];
return YES;
}
注意: 1ab37d5dsa3a3c 是你的 App Key
第四:根据api文档获取里面的字段和返回参数取值
他的返回参数是
{
"msg": "success",
"result": [
{
"airCondition": "良",
"city": "北京",
"coldIndex": "低发期",
"updateTime": "",
"date": "2015-09-08",
"distrct": "门头沟",
"dressingIndex": "短袖类",
"exerciseIndex": "适宜",
"future": [
{
"date": "2015-09-09",
"dayTime": "阵雨",
"night": "阴",
"temperature": "24°C/18°C",
"week": "星期三",
"wind": "无持续风向小于3级"
},
{
"date": "2015-09-10",
"dayTime": "阵雨",
"night": "阵雨",
"temperature": "22°C/15°C",
"week": "星期四",
"wind": "无持续风向小于3级"
},
{
"date": "2015-09-11",
"dayTime": "阴",
"night": "晴",
"temperature": "23°C/15°C",
"week": "星期五",
"wind": "北风3~4级无持续风向小于3级"
},
{
"date": "2015-09-12",
"dayTime": "晴",
"night": "晴",
"temperature": "26°C/13°C",
"week": "星期六",
"wind": "北风3~4级无持续风向小于3级"
},
{
"date": "2015-09-13",
"dayTime": "晴",
"night": "晴",
"temperature": "27°C/16°C",
"week": "星期日",
"wind": "无持续风向小于3级"
},
{
"date": "2015-09-14",
"dayTime": "晴",
"night": "多云",
"temperature": "27°C/16°C",
"week": "星期一",
"wind": "无持续风向小于3级"
},
{
"date": "2015-09-15",
"dayTime": "少云",
"night": "晴",
"temperature": "26°C/14°C",
"week": "星期二",
"wind": "南风3级南风2级"
},
{
"date": "2015-09-16",
"dayTime": "局部多云",
"night": "少云",
"temperature": "26°C/15°C",
"week": "星期三",
"wind": "南风3级南风2级"
},
{
"date": "2015-09-17",
"dayTime": "阴天",
"night": "局部多云",
"temperature": "26°C/15°C",
"week": "星期四",
"wind": "东南风2级"
}
],
"humidity": "湿度:46%",
"province": "北京",
"sunset": "18:37",
"sunrise": "05:49",
"temperature": "25℃",
"time": "14:35",
"washIndex": "不适宜",
"weather": "多云",
"week": "周二",
"wind": "南风2级"
}
],
"retCode": ""
}
最后:
//
// WeatherMainViewController.m
// 全球天气预报
//
// Created by txbydev3 on 17/1/11.
// Copyright © 2017年 陈竹青. All rights reserved.
// //屏幕的宽度
#define TXBYApplicationW ([UIScreen mainScreen].applicationFrame.size.width)
//屏幕的高度
#define TXBYApplicationH ([UIScreen mainScreen].applicationFrame.size.height) #import "WeatherMainViewController.h"
#import "WeatherViewController.h"
#import "Weather.h"
#import "FutureWeather.h"
#import "MJExtension.h"
#import <MobAPI/MobAPI.h>
#import <MOBFoundation/MOBFoundation.h> @interface WeatherMainViewController ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) NSArray *resultArray;
@property (nonatomic, strong) NSArray *futureArray; @property (nonatomic, strong) Weather *weather; @property (nonatomic, strong) UITableView *tableView ; @property (weak, nonatomic) UILabel *city;
@property (weak, nonatomic) UILabel *airCondition;
@property (weak, nonatomic) UILabel *temperature;
@property (weak, nonatomic) UILabel *weatherLab; @end @implementation WeatherMainViewController - (void)viewDidLoad {
[super viewDidLoad];
[self request ];
} - (void)setHeadView {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"background"];
[self.view addSubview:imageView];
// 城市
UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake((TXBYApplicationW - )*0.5, , , )];
cityLabel.textColor = [UIColor whiteColor];
cityLabel.font = [UIFont systemFontOfSize:];
[imageView addSubview:cityLabel];
self.city = cityLabel; // 空气指数
UILabel *air = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(cityLabel.frame) + , , )];
air.textColor = [UIColor whiteColor];
air.font = [UIFont systemFontOfSize:];
air.text = @"空气指数";
[imageView addSubview:air]; UILabel *airCondition = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(air.frame), CGRectGetMaxY(cityLabel.frame) + , TXBYApplicationW - , )];
airCondition.textColor = [UIColor whiteColor];
airCondition.font = [UIFont systemFontOfSize:];
[imageView addSubview:airCondition];
self.airCondition = airCondition; // 温度
UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(air.frame) + , , )];
temp.textColor = [UIColor whiteColor];
temp.text =@"温度";
temp.font = [UIFont systemFontOfSize:];
[imageView addSubview:temp]; UILabel *temperature = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(temp.frame) , CGRectGetMaxY(airCondition.frame) + , , )];
temperature.textColor = [UIColor whiteColor];
temperature.font = [UIFont systemFontOfSize:];
[imageView addSubview:temperature];
self.temperature = temperature; // 天气
UILabel *weatherLab = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(temp.frame) + , , )];
weatherLab.textColor = [UIColor whiteColor];
weatherLab.text =@"天气";
weatherLab.font = [UIFont systemFontOfSize:];
[imageView addSubview:weatherLab]; UILabel *weather = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(weatherLab.frame), CGRectGetMaxY(temperature.frame) + , , )];
weather.textColor = [UIColor whiteColor];
weather.font = [UIFont systemFontOfSize:];
// weather.textAlignment = NSTextAlignmentCenter;
[imageView addSubview:weather];
self.weatherLab = weather; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(weather.frame) + , TXBYApplicationW, TXBYApplicationH *0.5) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.tableFooterView = [UIView new];
[imageView addSubview:self.tableView];
} -(void)request {
//1.确定请求路径
NSString *urlStr = @"http://apicloud.mob.com/v1/weather/query?key=1ab5503423a3c&city=苏州&province=江苏";
// 防止 string 类型 转 url 为空
urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.获得会话对象
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
//6.解析服务器返回的数据
//说明:(此处返回的数据是JSON格式的,因此使用NSJSONSerialization进行反序列化处理)
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",dict);
[self setHeadView];
self.resultArray = [Weather mj_objectArrayWithKeyValuesArray:dict[@"result"]];
for (Weather *weather in self.resultArray) {
self.city.text = weather.city;
self.temperature.text = weather.temperature;
self.airCondition.text =weather.airCondition;
self.weatherLab.text =weather.weather;
self.futureArray = weather.future;
}
}
}];
//5.执行任务
[dataTask resume];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.futureArray.count;
} - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
}
FutureWeather *weather = self.futureArray[indexPath.row]; cell.textLabel.text = weather.week;
cell.detailTextLabel.text = weather.temperature;
return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end
效果图
根据Mob官网的天气预报接口写了一个简单的demo的更多相关文章
- 只是一个用EF写的一个简单的分页方法而已
只是一个用EF写的一个简单的分页方法而已 慢慢的写吧.比如,第一步,先把所有数据查询出来吧. //第一步. public IQueryable<UserInfo> LoadPagesFor ...
- 写了一个简单的CGI Server
之前看过一些开源程序的源码,也略微知道些Apache的CGI处理程序架构,于是用了一周时间,用C写了一个简单的CGI Server,代码算上头文件,一共1200行左右,难度中等偏上,小伙伴可以仔细看看 ...
- 自己写的一个简单PHP采集器
自己写的一个简单PHP采集器 <?php //**************************************************************** $url = &q ...
- 写了一个简单可用的IOC
根据<架构探险从零开始写javaweb框架>内容写的一个简单的 IOC 学习记录 只说明了主要的类,从上到下执行的流程,需要分清主次,无法每个类都说明,只是把整个主线流程说清楚,避免 ...
- 写了一个简单的 Mybatis
写了一个简单的 Mybatis,取名 SimpleMybatis . 具备增删改查的基本功能,后续还要添加剩下的基本数据类型和Java集合类型的处理. 脑图中有完整的源码和测试的地址 http://n ...
- vue3官网介绍,安装,创建一个vue实例
前言:这一章主要是vue的介绍.安装.以及如何创建一个vue实例. 一.vue介绍 vue3中文官网:建议先自己看官网. https://v3.cn.vuejs.org/ vue是渐进式框架,渐进式指 ...
- 写了一个简单的NodeJS实现的进程间通信的例子
1. cluster介绍 大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,有人开发了第三方的cluster,让node可以利用多核CPU ...
- 利用HttpClient写的一个简单页面获取
之前就听说过利用网络爬虫来获取页面,感觉还挺有意思的,要是能进行一下偏好搜索岂不是可以满足一下窥探欲. 后来从一本书上看到用HttpClient来爬取页面,虽然也有源码,但是也没说用的HttpClie ...
- 今天写了一个简单的新浪新闻RSS操作类库
今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些. 一年前我写过一个RSS阅读器,不过 ...
随机推荐
- 浅谈移动Web开发(上):深入概念
PPI 什么是PPI PPI的复杂之处在于如果他所属的上下文环境不同,意义也会完全不一样. 当我们在谈论显示设备的PPI时,它代指的屏幕的像素密度:当我们在谈论和图片相关时,我们谈论的是打印时的分辨率 ...
- javascript7
语句:条件,循环,跳转, 表达式语句,复合语句和空语句,声明语句,var,function,条件语句,switch,循环,标签语句,break语句,continue语句,return语句,throw语 ...
- 安卓CTS官方文档之兼容性测试套件简介
官方英文文档原文:https://source.android.com/compatibility/cts-intro.html Compatibility Test Suite 兼容性测试套件 Ho ...
- VC++.Net CAD编程架构
1.每个对应的菜单项的图形抽象的, 图形抽象基类, 取决于改变来自子(如矩形.椭圆形) 2.在Doc对象管理列表管理,图形对象,当图形需要重绘或序列存储,通过遍历该列表的对象可以是 3. 每个类的职责 ...
- SQL点滴26—常见T-SQL面试解析
原文:SQL点滴26-常见T-SQL面试解析 它山之石可以攻玉,这一篇是读别人的博客后写下的,不是原原本本的转载,加入了自己的分析过程和演练.sql语句可以解决很多的复杂业务,避免过多的项目代码,下面 ...
- C语言生成2000w行数据
最近一直抽空学习shell,脚本语言看多了多多少少有些蛋疼不适,所以捡起以前遇到的一个C语言的问题看看. 原先应该是在C++吧关注的一个帖子,楼主为了测试数据库性能需要如下形式的数据要求: 字符串长度 ...
- ubuntu忘记密码,无法sudo的解决方法
想要安装一个sublime Text Editor,发现需要root权限,而且sudo用户的密码输进去没有作用!@ubuntu 14.04 LTS 这个时候怎么办呢? 打开终端,在终端中使用 sudo ...
- C#实现文档转换成PDF
网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...
- 使用AdvancedInstaller打包web工程设置tomcat端口的方法
原文:使用AdvancedInstaller打包web工程设置tomcat端口的方法 1.首先,要把你要打包的tomcat下的server.xml文件删掉,因为tomcat自带的serv ...
- 地图坐标Base64转换数字坐标
测试地图源码==百读Demo<html> <head> </head> <body> <script type="text/javasc ...