简书地址:http://www.jianshu.com/p/a611ffb02ae7
github地址:https://github.com/GarveyBear/GBNetWorking(目前支持GET、POST、Download三种)
直接下载参照源码,使用之前记得pod AFNetWorking3.0进去噢!
cocoaPod使用:
- 打开终端 cd 你项目的路径
- vim Podfile
- 键盘输入 i,进入编辑模式,输入
platform :ios, ‘8.0’
pod ‘AFNetworking’, ‘~> 3.0’ - 按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq
- pod install
AFNetworking 3.0现已完全基于NSURLSession的API,这降低了维护的负担,同时支持苹果增强关于NSURLSession提供的任何额外功能。
提示:下面的类已从AFNetworking 3.0中废弃:
1. AFURLConnectionOperation
2. AFHTTPRequestOperation
3. AFHTTPRequestOperationManager
GBNetWorking 使用介绍:
注释:其中的get和post方法都将成功返回的数据转换成字典格式以便读取数据!
- 无类型区分的get请求
/**
* @param strURL 请求的链接
* @param dictionary 请求回调的数据
* @param error 请求失败回调
*/
NSString *strURL = @"http://api.worldweatheronline.com/free/v2/weather.ashx?q=Zhangzhou&num_of_days=1&format=json&tp=6&key=5f9fbd4fbbcae7c342d0978ca172e";//获取杭州的天气
[GBNetWorking GetRequest:strURL successBlock:^(NSDictionary * _Nullable dictionary)
{
GBLog(@"%@", dictionary);
} errorBlock:^(NSError * _Nullable error) {
GBLog(@"error %@", error);
}];
- 带类型区分的get请求
/**
* @param strURL 请求的链接
* @param dictionary 请求回调的数据
* @param error 请求失败回调
*/
[GBNetWorking GetRequestType:@"天气" Url:strURL successBlock:^(NSDictionary * _Nullable dictionary, NSString * _Nonnull type)
{
GBLog(@"%@ %@", type, dictionary);
} errorBlock:^(NSError * _Nullable error) {
GBLog(@"%@", error);
}];
- 无类型区分的post请求
/**
* @param strURLPost 请求的链接
* @param dic 请求上传的数据
* @param dictionary 请求回调的数据
* @param error 请求失败回调
*/
NSString *URLPost = [[NSString alloc] init];//需要上传的url
NSDictionary *dic = [[NSDictionary alloc] init];//上传的字典格式的参数
[GBNetWorking PostRequest:URLPost dicData:dic successBlock:^(NSDictionary * _Nullable dictionary) {
GBLog(@"%@", dictionary);
} errorBlock:^(NSError * _Nullable error)
{
GBLog(@"%@", error);
}];
- 带类型区分的post请求
/**
* @param URLTYPE 请求的链接
* @param str 请求的类型
* @param dicType 请求上传的数据
* @param dictionary 请求回调的数据
* @param error 请求失败回调
*/
NSString *URLTYPE = [[NSString alloc] init];
NSString *str = [[NSString alloc] init];
NSDictionary *dicType = [[NSDictionary alloc] init];
[GBNetWorking PostRequestType:str Url:URLTYPE dicData:dicType successPBlock:^(NSDictionary * _Nullable dictionary, NSString * _Nonnull type)
{
GBLog(@"%@ %@", type, dictionary);
} errorBlock:^(NSError * _Nullable error)
{
GBLog(@"%@", error);
}];
- 下载
/**
* @param URLString 请求的链接
* @param progress 进度的回调
* @param destination 返回URL的回调
* @param downLoadSuccess 发送成功的回调
* @param failure 发送失败的回调
*/
NSString *URLString = @"http://images.apple.com/v/iphone-5s/gallery/a/images/download/photo_1.jpg";
__block UIProgressView *prossView = _proGressView;//UIProgressView 显示的控件
__block UILabel *prossLb = _proLb;//UILabel 显示的控件
[[GBNetWorking new] downLoadWithURL:URLString progress:^(NSProgress * _Nullable progress) {
GBLog(@"progress == %@", progress);
GBLog(@"%lf", 1.0 * progress.completedUnitCount / progress.totalUnitCount);
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
prossView.progress = progress.completedUnitCount / progress.totalUnitCount;
float _percent = prossView.progress;
CFLocaleRef currentLocale = CFLocaleCopyCurrent();
CFNumberFormatterRef numberFormatter = CFNumberFormatterCreate(NULL, currentLocale, kCFNumberFormatterPercentStyle);
CFNumberRef number = CFNumberCreate(NULL, kCFNumberFloatType, &_percent);
CFStringRef numberString = CFNumberFormatterCreateStringWithNumber(NULL, numberFormatter, number);
prossLb.text = (__bridge NSString *)numberString;
});
} destination:^NSURL * _Nullable(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
GBLog(@"targetPath == %@, response == %@", targetPath, response);
// 设置下载路径,通过沙盒获取缓存地址,最后返回NSURL对象
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"hhhh.mp3"];
return [NSURL fileURLWithPath:filePath];
} downLoadSuccess:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath) {
GBLog(@"下载完成\n%@--%@",response, filePath);
} errorBlock:^(NSError * _Nullable error)
{
GBLog(@"error %@", error);
}];