天气预报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>
- (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
效果图
天气预报demo (ShareREC 官网 MobAPI)的更多相关文章
- Java微信扫描支付模式二Demo ,整合官网直接运行版本
概述 场景介绍 用户使用微信“扫一扫”扫描二维码后,获取商品支付信息,引导用户完成支付. 详细 代码下载:http://www.demodashi.com/demo/13880.html 一.相关配置 ...
- spring boot 快速生成demo工程 官网生成
最近一直在弄springboot的项目,居然最近才知道快速生成springBoot工程,原来可以这么简单, 而且官网还提供了生成java或是web项目,需要jpa,模板等服务,直接一键集成.话不多说, ...
- 如何运行Struts2官网最新Demo?
本篇将讲述下如何运行官网当前最新Struts2.5.10.1 版本的Demo. Struts2 官网:http://struts.apache.org/ 0x00 Demo下载 Struts2 官网2 ...
- 根据Mob官网的天气预报接口写了一个简单的demo
第一步 自己注册一个应用,然后获取里面的 App Key,下载MobAPI SDK 然后拖入 MobAPI.framework 和 MOBFoundation.framework 到你的项目中 第二步 ...
- SpringBoot使用JSP(官网Demo)
最开始接触java的时候,前端页面基本都是用jsp来写,最近公司项目要使用SpringBoot重构,查看SpringBoot文档,发现SpringBoot不建议使用JSP,因为jsp在使用内嵌serv ...
- Java 银联支付官网demo测试及项目整合代码
注:原文来源与 < Java 银联支付官网demo测试及项目整合代码 > 银联支付(网关支付B2C) 一.测试官网demo a)下载官网开发包,导入eclipse等待修改(下载的开发包没 ...
- dubbo系列一:dubbo介绍、dubbo架构、dubbo的官网入门使用demo
一.dubbo介绍 Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成.简单地说,dubbo是一个基于Spri ...
- React官网首页demo(单文件实现版)
本博客实现React官网首页上展示的demo, 为了方便直接采用单文件的形式, 如果想完整集成 在自己的项目中, 可以参考React官网的安装指南, 安装Create React App. hello ...
- jqgrid--api,官网demo,编辑
api参考: http://blog.csdn.net/hurryjiang/article/details/7551477 官网demo: http://www.trirand.com/blog/j ...
随机推荐
- python之函数递归
函数递归调用 在函数内部,可以调用其它函数,如果一个函数在内部调用自身,即是递归调用 为防止无限递归类似于死循环,需要如下: 1.必须要有一个明确的返回值: 2.每次进入更深一层递归时,问题规模应该比 ...
- linux搭建zabbix server
一.linux配置jdk 1.安装rpm包,安装完成位置:/usr/java/jdk1.8.0_152 2.配置环境变量/etc/profile: JAVA_HOME=/usr/java/jdk1.. ...
- 解决cpplint在Python 3下没有任何输出的问题
修改cpplint.py:1. main()中注释掉 # sys.stderr = codecs.StreamReaderWriter(sys.stderr, # codecs.getreader ...
- 小强学渲染之Unity Shader噪声应用
之前玩Tencent的仙剑4手游时,杀死boss会看到boss有“消融”的效果,就是身体上有多个洞洞然后往四周扩散直至尸体完全消失,但效果是没有关闭背面剔除的“穿帮”效果,可能也是考虑性能因素. em ...
- ViewPager和Fragment中的View的点击事件冲突
ViewPager属于父布局,View属于子布局: 触摸事件是先到父View,再到子View,所以可以让ViewPager取消拦截事件: public class ComposeViewPager e ...
- linux systemctl 命令学习
转载:http://www.cnblogs.com/sparkdev/p/8472711.html systemctl 命令有两大类功能: 控制 systemd 系统 管理系统上运行的服务 在介绍这些 ...
- MR汇聚工具步骤
---------------------------------MR汇聚工具步骤------------------------------------- 1.需要连上141服务器 用户:root ...
- php操作redis数据库方法总结
一.安装php_redis扩展,用以操作redis http://pecl.php.net/package/redis 选择自已系统php版本对应的扩展. 二.redis连接与验证 <?php ...
- node.js 使用NAPI写C++插件,(部分转帖)
原文:https://www.cnblogs.com/chyingp/p/nodejs-learning-napi.html 可能是版本问题,我用node v10.6.0 遇到了问题 不过解决了. 一 ...
- xtrabackup命令用法实战(转)
xtrabackup命令用法实战 转载出自 https://blog.csdn.net/wfs1994/article/details/80399408 完全备份 1.创建备份 [root@linux ...