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. jquery 获取字符串中的数字

    str_num = 'abc123' num = parseInt(str_num.replace(/[^0-9]/ig,"")); alert(num);

  2. session监听器HttpSessionBindingListener

    首先我在网上查了一下session的真正销毁条件: 1调用 session.invalidate();方法 2 session到了设置或者默认的超时时间,自动销毁(关闭浏览器此session还未销毁, ...

  3. am335x usb host patch设计

    USB直接作为host时省掉一个5V的DCDC,直接连接到5V的输入上面.对于Linux SDK,需要一个patch去确保这个设计能够正常工作,patch内容请参考: static struct om ...

  4. Java中的阻塞队列(BlockingQueue)

    1. 什么是阻塞队列 阻塞队列(BlockingQueue)是 Java 5 并发新特性中的内容,阻塞队列的接口是 java.util.concurrent.BlockingQueue,它提供了两个附 ...

  5. SQL Server 数据库同步,订阅、发布、复制、跨服务器

    随便说两句 折腾了一周,也算把数据库同步弄好了.首先局域网内搭建好,进行各种测试,弄的时候各种问题,弄好以后感觉还是挺简单的.本地测试好了,又在服务器进行测试,主要的难点就是跨网段同步,最后也解决了, ...

  6. Windows 使用 Gitblit 搭建 Git 服务器

    吐槽 Windows 安装 Git 那真是各种问题层出不穷,试过N种方法,反反复复不知道装了多少遍,然后又卸载.最后使用了Gitblit搭建Git服务器,虽然也遇到一些问题,但是都解决了.这个软件其实 ...

  7. go hmac使用

    https://github.com/danharper/hmac-examples 94 func generateSign(data, key []byte) string { 95 mac := ...

  8. Java 07 example

    留下两个例子作为参考, 1. 追逐小方块的例子 2. HashMap 和 Iterator 的例子 Example one: import acm.graphics.*; import acm.pro ...

  9. MyBatis 本是apache的一个开源项目iBatis

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  10. Struts2_day03--从值栈获取数据_EL表达式获取值栈数据(为什么)

    从值栈获取数据 1 使用struts2的标签+ognl表达式获取值栈数据 (1)<s:property value=”ognl表达式”/> 获取字符串 1 向值栈放字符串 2 在jsp使用 ...