天气预报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
效果图
转载于:https://www.cnblogs.com/chenzq12/p/6274204.html
天气预报demo (ShareREC 官网 MobAPI)相关推荐
- timeAxis.js--一个简单的时间轴JS框架--仿苹果官网
代码已经上传到Github,希望各位大佬指教. ReadMe还是以GitHub为准 GitHub地址:https://github.com/royalknight56/timeAxis.js 基本实现 ...
- 新手入坑:strapi官网教程的简单示例学习
关于strapi 前端仔,来,看这里,用strapi,定制你的api.一个字,香. 安装 npm i strapi@beta -g strapi new str-project 可能会比较慢 开始 n ...
- Spring Batch_官网DEMO实现
2019独角兽企业重金招聘Python工程师标准>>> Spring Batch_官网DEMO实现 http://spring.io/guides/gs/batch-processi ...
- 今天,神策数据官网银行 Demo 正式上线!
目前,招商银行.中原银行.百信银行等银行正在使用神策分析.我们身边的这些银行是如何挖掘数据价值的呢? 神策数据官网银行 Demo 今天正式上线!这片试验田将带你初步感受这些"客户满意银行&q ...
- combobox 怎么实现对listview的类别查询_Flutter实战之独立实现官网Demo单词收藏Demo
概述 这是一次独立尝试,即独立完成实现Flutter文档第一个项目的最终效果,实现目标即首页为一个可无限滚动的单词列表,点击单词右边的小心心即可收藏或者取消收藏,可以在右上角的列表页查看收藏的单词,自 ...
- python天气预报官网_python 实现对天气预报官网未来一周七天的天气情况抓取
通过HTMLPaser和urlib模块对网页进行抓取并分析 实现步骤: 1.自定义MYHTMLParser类 2.实例化类并访问天气预报官网 3.抓取关键数据 4.对数据进行切片处理并输出字典 fro ...
- android sdk 官网说明,神目人脸识别Android SDK Demo说明
Demo使用说明 SDK Demo主界面如图1-1所示,主要功能有:1:1,1:N,人脸库管理,设置选项四大功能.具体说明如下: (1)1:1,即图片1与图片2进行人脸比对,得出两者的相似度分数.界面 ...
- Java 银联支付官网demo测试及项目整合代码
银联支付(网关支付B2C) 一.测试官网demo a)下载官网开发包,导入eclipse等待修改(下载的开发包没有servlet jar包自己在Tomcat找一个) b)进入银联官网账户https:/ ...
- kurento 6.6.0 安装部署 + 运行官网demo
文章所有均在VirtualBox+Ubuntu14.04上进行的. 环境和工具:Java 8.Maven 3.5.0.Node.js 0.10.48.Bower.git. 环境和工具不同,不保证能够正 ...
最新文章
- html中如何实现选择存储路径的功能_HTML是Web前端的基础知识,刚入门的你真的学对了吗?...
- Python解析xml文件,此实例将xml设置为模版(from lxml import etree)
- 荣耀鸿蒙系统内测,官宣!荣耀 Magic UI 4.0 与 EMUI 11 同步内测:后续支持升级为鸿蒙操作系统...
- NSOperation 开发
- ubuntu16.04中创建新用户
- WinCE设备仿真器+虚拟串口+GPS模拟器搭建开发测试环境
- 用MATLAB 读写各种文件 ∈ Matlab 使用笔记
- Python中整除、求余运算
- word文档添加多个目录
- 电感耦合等离子体发射光谱法测定水样中的金属含量
- 如何应对团队协作的五大障碍
- HTPPS和HTTP的概念和区别
- Redmi MAX 90 评测 Redmi MAX 90参数
- Vue后台--Ele组件表格根据数字展示数值
- 数学建模之线性回归的标准型以及例题
- anki server 错误与解决
- source insight 仿 vscode 主题 (GitHub免积分下载)
- ubuntu下手动安装gnome插件
- 外汇超短线交易中的“剥头皮”
- 字符串编码(utf8)
热门文章
- 13.EVE-NG内存不够?教您扩大虚拟内存!
- python环境搭建心得_python第一课 基本语法搭建以及环境
- 关于增删改查自己的心得
- js的for循环语句
- 2023华为产品测评官-开发者之声 + 华为云ModelArts试用体验心得
- Flutter 状态管理
- VS2019创建SDL2工程,以及找不到SDL2.dll
- No MyBatis mapper was found in 'XXX' package. Please check your configuration
- 读书笔记-伟人传记不适合教育小孩
- 1182_SPC560B60L7_ADC的CTU触发功能简介