通过 CocoaPods
pod "PbApi"
在 PBApi 中使用时,每个 API 即为一个类,继承自 PbApiBase 类,并重写指定方法来配置 API。
建议创建自己的基类来配置基本信息,如设置服务器域名、设置请求的 header、设置 API 的前缀(如 /api/v1)。
例如创建基类名为 MyApiBase
基类 h 文件
#import "PbApi.h"
@interface MyApiBase : PbApiBase
@end
基类 m 文件
#import "MyApiBase.h"
@implementation MyApiBase
/*
必须重写的方法
设置api的域名,需要带http:// 或 https://
*/
- (NSString *)domain {
return @"http://api.mydomain.com";
}
/*
url前缀
*/
- (NSString *)prefix {
return @"api/v1";
}
/*
url后缀
*/
- (NSString*)suffix {
return @".json";
}
/*
请求的头数据
*/
- (NSDictionary*)header {
return @{@"Authorization": @"Token xxx"};
}
/*
当请求失败时,将会有此回调,用于检查错误码
*/
- (void)didRequestFailureWithResponse:(NSHTTPURLResponse*)response error:(NSError*)error {
NSInteger statusCode = response.statusCode;
...
}
@end
例如创建用于请求用户信息的 API,基于基类 MyApiBase。
Api h 文件
#import "MyApiBase.h"
@interface MyApiGetUserInfo : MyApiBase
@end
Api m 文件
#import "MyApiGetUserInfo.h"
@implementation MyApiGetUserInfo
/*
必须重写的方法
设置api名
*/
- (NSString *)name {
return @"user_info";
}
/*
配置默认的请求方法,也可以不重写此方法来使用默认的PbRequestMethodGet
*/
- (PbRequestMethod)method {
return PbRequestMethodGet;
}
/*
将请求成功之后的json转换成自定义的格式并返回。例如转换为自定义的Model,也可以不重写此方法来默认使用json作为结果返回
*/
- (NSDictionary*)result:(NSDictionary*)json {
...
return json;
}
/*
以下为回调事件
*/
- (void)willRequestWithURLString:(NSString*)URLString {
[super willRequestWithURLString:URLString];
}
- (void)didRequestSuccessWithResult:(NSDictionary*)result response:(NSHTTPURLResponse*)response {
[super didRequestSuccessWithResult:result response:response];
}
- (void)didRequestFailureWithResponse:(NSHTTPURLResponse*)response error:(NSError*)error {
[super didRequestFailureWithResponse:response error:error];
}
@end
调用 Api
使用默认方法请求,默认请求方法为类中配置的 PbRequestMethodGet。
[MyApiGetUserInfo requestWithParam:@{@"username": @"admin"} completion:^(NSDictionary *result, NSError *error) {
if (error) {
return;
}
...
}];
使用指定方法请求
此时将忽略 Api 类中配置的方法
[MyApiGetUserInfo getWithParam:@{@"username": @"admin"} completion:^(NSDictionary *result, NSError *error) {
if (error) {
return;
}
...
}];