AFNetWork中使用https
AFNetWork这个库,是现在大家使用比较用的库,我们在做即时通讯的时候,也会加入这个库,用起来还是比较爽的。
有时候我们会用到https,在AFNetwork这个库,其实已经很好的封装了https,是的我们很简单就能使用。
第一步,导入AFNetWorking 库
第二步,在pch文件中加入
[objc] view plain copy
在CODE上查看代码片派生到我的代码片
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
#define AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES
第三步:
[objc] view plain copy
在CODE上查看代码片派生到我的代码片
- (void)inithttps{
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
securityPolicy.allowInvalidCertificates = YES;
[AFHTTPRequestOperationManager manager].securityPolicy = securityPolicy;}
这样就可以在程序中使用AFNetWorking来进行https操作了。
基于AFNetworking的再封装
@interface UploadParam : NSObject
/**
* 图片的二进制数据
*/
@property (nonatomic, strong) NSData *data;
/**
* 服务器对应的参数名称
*/
@property (nonatomic, copy) NSString *name;
/**
* 文件的名称(上传到服务器后,服务器保存的文件名)
*/
@property (nonatomic, copy) NSString *filename;
/**
* 文件的MIME类型(image/png,image/jpg等)
*/
@property (nonatomic, copy) NSString *mimeType;
@end
@class UploadParam;
/**
* 网络请求类型
*/
typedef NS_ENUM(NSUInteger,HttpRequestType) {
/**
* get请求
*/
HttpRequestTypeGet = 0,
/**
* post请求
*/
HttpRequestTypePost
};
@interface HttpRequest : NSObject
/**
* 发送get请求
*
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
*/
+ (void)getWithURLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 发送post请求
*
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
*/
+ (void)postWithURLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 发送网络请求
*
* @param URLString 请求的网址字符串
* @param parameters 请求的参数
* @param type 请求的类型
* @param resultBlock 请求的结果
*/
+ (void)requestWithURLString:(NSString *)URLString
parameters:(id)parameters
type:(HttpRequestType)type
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
/**
* 上传图片
*
* @param URLString 上传图片的网址字符串
* @param parameters 上传图片的参数
* @param uploadParam 上传图片的信息
* @param success 上传成功的回调
* @param failure 上传失败的回调
*/
+ (void)uploadWithURLString:(NSString *)URLString
parameters:(id)parameters
uploadParam:(UploadParam *)uploadParam
success:(void (^)())success
failure:(void (^)(NSError *error))failure;
#import "HttpRequest.h"
#import "AFNetworking.h"
#import "UploadParam.h"
@implementation HttpRequest
#pragma mark -- GET请求 --
+ (void)getWithURLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id))success
failure:(void (^)(NSError *))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
/**
* 可以接受的类型
*/
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
/**
* 请求队列的最大并发数
*/
// manager.operationQueue.maxConcurrentOperationCount = 5;
/**
* 请求超时的时间
*/
// manager.requestSerializer.timeoutInterval = 5;
[manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
#pragma mark -- POST请求 --
+ (void)postWithURLString:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id))success
failure:(void (^)(NSError *))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
#pragma mark -- POST/GET网络请求 --
+ (void)requestWithURLString:(NSString *)URLString
parameters:(id)parameters
type:(HttpRequestType)type
success:(void (^)(id))success
failure:(void (^)(NSError *))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
switch (type) {
case HttpRequestTypeGet:
{
[manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
break;
case HttpRequestTypePost:
{
[manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
break;
}
}
#pragma mark -- 上传图片 --
+ (void)uploadWithURLString:(NSString *)URLString
parameters:(id)parameters
uploadParam:(UploadParam *)uploadParam
success:(void (^)())success
failure:(void (^)(NSError *))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:uploadParam.data name:uploadParam.name fileName:uploadParam.filename mimeType:uploadParam.mimeType];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}