首先创建两个block
typedef void(^DownloadSucceedBlock)(id responseObject); //成功后数据
typedef void(^DownloadFaildedBlock)(NSError *error); //失败信息
#pragma mark 基础数据申请
- (void)URLManagerWithUrlStr:(NSString *)urlStr succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock;
#pragma mark 基础数据申请
- (void)URLManagerWithUrlStr:(NSString *)urlStr succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock {
// 设置数据申请
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
// 根据网络申请创建操作
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// 设置相应并串行转换器
operation.responseSerializer = [AFJSONResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
// 进行数据申请
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
succeedBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failedBlock(error);
}];
// 将操作加入主队列
[[NSOperationQueue mainQueue] addOperation:operation];
}
#pragma mark get请求方法
- (void)getMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock;
#pragma mark get请求方法
- (void)getMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
succeedBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failedBlock(error);
}];
}
#pragma mark post请求方法
- (void)postMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock;
#pragma mark post请求方法
- (void)postMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
succeedBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failedBlock(error);
}];
}
#pragma mark 本地文件/图片上传
- (void)postMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters filePath:(NSString *)filePath succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock;
#pragma mark 本地文件/图片上传
- (void)postMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters filePath:(NSString *)filePath succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURL *filePathUrl = [NSURL fileURLWithPath:filePath];
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePathUrl name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
succeedBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failedBlock(error);
}];
}
#pragma mark 图片数据上传/可用于图片的多张上传
- (void)postMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters imageData:(NSData *)imagedata fileName:(NSString *)fileName succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock;
#pragma mark 图片数据上传/可用于图片的多张上传
- (void)postMethodWithURL:(NSString *)url parameters:(NSDictionary *)parameters imageData:(NSData *)imagedata fileName:(NSString *)fileName succeed:(DownloadSucceedBlock)succeedBlock andFaild:(DownloadFaildedBlock)failedBlock {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.requestSerializer = [AFHTTPRequestSerializer serializer]; //初始化
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //初始化
manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
manager.responseSerializer = [AFJSONResponseSerializer serializer];//响应
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imagedata name:@"file" fileName:fileName mimeType:@"image/jpg/Documents"];
// [formData appendPartWithFormData:imagedata name:@"image"]; //估计是传一个数据
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
succeedBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failedBlock(error);
}];
}
#pragma mark 创建下载任务
- (void)downloadTaskWithUrlStr:(NSString *)urlStr;
#pragma mark 创建下载任务
- (void)downloadTaskWithUrlStr:(NSString *)urlStr {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
}
#pragma mark 创建上传任务
- (void)uploadTaskWithUrlStr:(NSString *)urlStr filePathStr:(NSString *)filePathStr;
#pragma mark 创建上传任务
- (void)uploadTaskWithUrlStr:(NSString *)urlStr filePathStr:(NSString *)filePathStr {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:filePathStr];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
}
#pragma mark 数据申请管理
- (void)HTTPReachabiilityWithUrlStr:(NSString *)urlsStr;
#pragma mark 数据申请管理
- (void)HTTPReachabiilityWithUrlStr:(NSString *)urlsStr {
NSURL *baseURL = [NSURL URLWithString:urlsStr];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES];
break;
}
}];
[manager.reachabilityManager startMonitoring];
}