A.封装微博业务
1.需求
把微博相关业务(读取、写微博)
界面控制器不需要知道微博操作细节(例如选择从网络读取还是缓存读取)
 
2.实现
把微博操作封装成一个工具类
把微博网络请求的参数和返回结果也封装成一个类
 
 
 
3.实现
(1)基础参数类
由于多数请求都需要access_token,所以封装一个参数父类
 //
// HVWBaseParam.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWBaseParam : NSObject /** 访问令牌 */
@property(nonatomic, copy) NSString *access_token; @end
 
 //
// HVWBaseParam.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWBaseParam.h"
#import "HVWAccountInfo.h"
#import "HVWAccountInfoTool.h" @implementation HVWBaseParam - (NSString *)access_token {
if (nil == _access_token) {
_access_token = [HVWAccountInfoTool accountInfo].access_token;
}
return _access_token;
} @end
 
(2)首页获取微博
a.参数类
根据微博API请求参数列表
 
 //
// HVWHomeStatusParam.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWBaseParam.h" @interface HVWHomeStatusParam : HVWBaseParam /** false string 采用OAuth授权方式不需要此参数,其他授权方式为必填参数,数值为应用的AppKey。 */
@property(nonatomic, copy) NSString *source; /** false int64 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0。 */
@property(nonatomic, strong) NSNumber *since_id; /** false int64 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。 */
@property(nonatomic, strong) NSNumber *max_id; /** false int 单页返回的记录条数,最大不超过100,默认为20。 */
@property(nonatomic, strong) NSNumber *count; /** false int 返回结果的页码,默认为1。 */
@property(nonatomic, strong) NSNumber *page; /** false int 是否只获取当前应用的数据。0为否(所有数据),1为是(仅当前应用),默认为0。 */
@property(nonatomic, strong) NSNumber *base_app; /** false int 过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。 */
@property(nonatomic, strong) NSNumber *feature; /** false int 返回值中user字段开关,0:返回完整user字段、1:user字段仅返回user_id,默认为0。 */
@property(nonatomic, strong) NSNumber *trim_user; @end
 
b.返回结果类
微博返回结果是N条微博数据的数组
(这里在返回结果包装类中直接使用之前创建的HVWStatus类来封装微博数据)
 
每个status里面的元素:
 
 //
// HVWHomeStatusResult.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "HVWStatus.h" @interface HVWHomeStatusResult : NSObject /** 微博数组,里面装的HVWStatus模型 */
@property(nonatomic, strong) NSArray *statuses; /** 近期微博总数 */
@property(nonatomic, assign) int total_number; @end
 
 //
// HVWHomeStatusResult.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWHomeStatusResult.h"
#import "MJExtension.h" @implementation HVWHomeStatusResult /** 指明将json数组元素转为什么模型类 */
- (NSDictionary *)objectClassInArray {
return @{@"statuses":[HVWStatus class]};
} @end
 
c.公共请求工具类
内部处理模型转字典的逻辑
 //
// HVWBaseTool.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/10.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWBaseTool : NSObject /** GET请求 */
+ (void) getWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure; /** POST请求(不带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure; /** POST请求(带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters filesData:(NSArray *)filesData resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure; @end
 
 //
// HVWBaseTool.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/10.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWBaseTool.h"
#import "HVWNetworkTool.h"
#import "MJExtension.h" @implementation HVWBaseTool /** GET请求 */
+ (void) getWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure { // 解析出参数
NSDictionary *param = [parameters keyValues]; [HVWNetworkTool get:url parameters:param success:^(id responseObject) {
if (success) {
id result = [resultClass objectWithKeyValues:responseObject];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
if (failure) {
failure(error);
}
}
}];
} /** POST请求(不带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure { // 解析出参数
NSDictionary *param = [parameters keyValues]; [HVWNetworkTool post:url parameters:param success:^(id responseObject) {
if (success) {
id result = [resultClass objectWithKeyValues:responseObject];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
if (failure) {
failure(error);
}
}
}];
} /** POST请求(带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters filesData:(NSArray *)filesData resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure { // 解析出参数
NSDictionary *param = [parameters keyValues]; [HVWNetworkTool post:url parameters:param filesData:filesData success:^(id responseObject) {
if (success) {
id result = [resultClass objectWithKeyValues:responseObject];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
if (failure) {
failure(error);
}
}
}];
} @end
 
d.微博请求工具类
 //
// HVWStatusTool.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "HVWBaseTool.h"
#import "HVWHomeStatusParam.h"
#import "HVWHomeStatusResult.h" @interface HVWStatusTool : HVWBaseTool /** 获取首页微博数据 */
+ (void) statusWithParameters:(HVWHomeStatusParam *)parameters success:(void (^)(HVWHomeStatusResult *statusResult))success failure:(void (^)(NSError *error))failure; @end
 
 //
// HVWStatusTool.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWStatusTool.h"
#import "MJExtension.h" @implementation HVWStatusTool /** 获取首页微博数据 */
+ (void) statusWithParameters:(HVWHomeStatusParam *)parameters success:(void (^)(HVWHomeStatusResult *statusResult))success failure:(void (^)(NSError *error))failure {
// 发送请求
[self getWithUrl:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:parameters resultClass:[HVWHomeStatusResult class] success:success failure:failure];
} @end
 
==》以此类推,可以封装“发送微博”、“获取用户信息”等,其实就是一个微博API请求封装一套(参数 、结果和请求工具方法)
 
 

[iOS微博项目 - 3.5] - 封装业务的更多相关文章

  1. [iOS微博项目 - 2.5] - 封装授权和用户信息读写业务

    github: https://github.com/hellovoidworld/HVWWeibo   A.封装授权业务 1.把app的授权信息移动到HVWWeibo-Prefix.pch中作为公共 ...

  2. [iOS微博项目 - 3.3] - 封装网络请求

    github: https://github.com/hellovoidworld/HVWWeibo   A.封装网络请求 1.需求 为了避免代码冗余和对于AFN框架的多处使用导致耦合性太强,所以把网 ...

  3. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

  4. [iOS微博项目 - 3.4] - 获取用户信息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取用户信息 1.需求 获取用户信息并储存 把用户昵称显示在“首页”界面导航栏的标题上   ...

  5. [iOS微博项目 - 3.1] - 发微博界面

    github: https://github.com/hellovoidworld/HVWWeibo   A.发微博界面:自定义UITextView 1.需求 用UITextView做一个编写微博的输 ...

  6. [iOS微博项目 - 2.6] - 获取微博数据

    github: https://github.com/hellovoidworld/HVWWeibo   A.新浪获取微博API 1.读取微博API     2.“statuses/home_time ...

  7. [iOS微博项目 - 1.0] - 搭建基本框架

    A.搭建基本环境   github: https://github.com/hellovoidworld/HVWWeibo   项目结构:   1.使用代码构建UI,不使用storyboard     ...

  8. [iOS微博项目 - 4.6] - 微博配图

    github: https://github.com/hellovoidworld/HVWWeibo A.微博配图 1.需求 显示原创微博.转发微博的缩略图 4张图使用2x2布局,其他使用3x3布局, ...

  9. [iOS微博项目 - 4.0] - 自定义微博cell

    github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内 ...

随机推荐

  1. C++中explicit的用法

    https://blog.csdn.net/qq_35524916/article/details/58178072 https://blog.csdn.net/jinjin1062495199/ar ...

  2. UIViewController的生命周期及iOS程序执行顺序 和ios6 处理内存警告

    当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序1. alloc                                   创建对象,分配空间2.init (initWithN ...

  3. char数组与string转换

    1.char数组转string 有很多种方法: 假设c字符串定义为char ch[]="hello world!"; 1.向构造函数传入c字符串创建string对象: string ...

  4. 关闭IOS更新功能(ios4/5/6)

    防止IOS升级: 工具:ifunbox 展开/System/Library/LaunchDaemons,将下面4个文件删除(不推荐)或者改名(后缀也得改),改名后记得必须重启. com.apple.m ...

  5. GDB和WinDbg中调用函数

    GDB: 特别简单,直接写调用式子即可,如下图的p word.c_str(),其中word的类型是std::string WinDbg:目前都说是.call命令,说实话我宁愿不用...见: http: ...

  6. GPIO—位带操作

    GPIO—位带操作本章参考资料:< STM32F4xx 中文参考手册>存储器和总线构架章节. GPIO 章节,< Cortex®-M4 内核编程手册> 2.2.5 Bit-ba ...

  7. cocos2dx对所有子节点设置透明度

    看到cocos2dx2.2.5发布了,修复了输入框的bug,于是我们的项目也升级到了2.2.5, 升级过程还是比较顺利,没想到后来发现设置透明度无效了. 经过调试发现要调用一下setCascadeOp ...

  8. Eclipse中复制android项目后要改动的地方

    1.清单文件中,改package=2.修改包名3.清单文件中app_name F3点进去修改名字

  9. 如何测试是否安装了web服务器

    windows默认没有安装web服务器,我们可以安装IIS. 我们安装个tomacte服务器,开发web程序必须的!!如果测试后出现这个页面说明安装成功le ! 我们这个安装的是本地服务器,可以把we ...

  10. 使用tcpdump观察IPV4头部结构

    sudo tcpdump -nt -i lo  #抓取本地回路上的数据包 先运行上面的命令,然后再另一个终端运行下图所示的命令: [root@linux 5]# sudo tcpdump -nt -i ...