iOS疯狂详解之ASIHttpRequest的简单封装

//  iOS疯狂详解之ASIHttpRequest的简单封装

//  WLHTTPClient.h

//  WLWLListen

//

//  Created by long on 14-12-15.

//  Copyright (c) 2014年 WLong. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "ASIFormDataRequest.h"

@protocol WLHTTPClientDelegate<NSObject>

@optional

@end

@interface WLHTTPClient : NSObject

@property (nonatomic,assign) id<WLHTTPClientDelegate>delegate;

+ (WLHTTPClient *)sharedClient;

- (void)postInfoWithDelegate:(id<WLHTTPClientDelegate>)delegate

withMethod:(NSString *)method

withParams:(NSDictionary *)params

withSuccuessMethod:(SEL )successMethod

withFailedMethod:(SEL )failMethod;

@end

//

//  WLHTTPClient.m

//  WLVkoListen

//

//  Created by long on 14-12-15.

//  Copyright (c) 2014年 WLong. All rights reserved.

//

#import "WLHTTPClient.h"

#define SuppressPerformSelectorLeakWarning(Stuff) \

do { \

_Pragma("clang diagnostic push") \

_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \

Stuff; \

_Pragma("clang diagnostic pop") \

} while (0)

@implementation WLHTTPClient

+ (WLHTTPClient *)sharedClient

{

static WLHTTPClient *_sharedClient = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_sharedClient = [[WLHTTPClient alloc]init];

});

return _sharedClient;

}

- (void)postInfoWithDelegate:(id<WLHTTPClientDelegate>)delegate

withMethod:(NSString *)method

withParams:(NSDictionary *)params

withSuccuessMethod:(SEL )successMethod

withFailedMethod:(SEL )failMethod

{

NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseHttpUrl,method];

__weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];

[request setRequestMethod:@"POST"];

for (int i = 0;  i < [params allKeys].count; i++) {

[request addPostValue:params[[params allKeys][i]] forKey:[params allKeys][i]];

}

[request setCompletionBlock:^{

NSData *jsonData = request.responseData;

id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

SuppressPerformSelectorLeakWarning(

[delegate performSelector:successMethod withObject:result];

);

}];

[request setFailedBlock:^{

SuppressPerformSelectorLeakWarning(

[delegate performSelector:failMethod withObject:@"网络连接失败,请检查您的网络连接或重试!"];

);

}];

[request startAsynchronous];

}

@end

更多iOS疯狂详解:http://blog.csdn.net/wanglongblog

时间: 2024-08-24 03:50:53

iOS疯狂详解之ASIHttpRequest的简单封装的相关文章

iOS疯狂详解之CocoaPods做iOS程序包的依赖管理

每种语言发展到一个阶段,就会出现相应的依赖管理工具, 或者是中央代码仓库.比如 Java: maven,Ivy Ruby: gems Python: pip, easy_install Nodejs: npm 随着iOS开发者的增多,业界也出现了为iOS程序提供依赖管理的工具,这个工具叫:CocoaPods. CocoaPods简介 CocoaPods是一个负责管理iOS项目中第三方开源代码的工具.CocoaPods项目的源码在Github上管理.该项目开始于2011年8月12日,经过一年多的发

iOS疯狂详解之第三方微信授权登录的iOS代码分析

微信已经深入到每一个APP的缝隙,最常用的莫过分享和登录了,接下来就以代码的形式来展开微信登录的相关说明,至于原理级别的oauth2.0认证体系请参考微信开放平台的相关说明和图示 https://open.weixin.qq.com/ 微信登录授权开发 1,到微信开发平台注册相关APP,现在是等待审核成功后才能获取到对应的key和secret:获取成功后需要单独申请开通登录和支付接口,如图 2,和QQ类似,需要填写Url Schemes,如demo中的wxd930ea5d5a258f4f ,然后

iOS疯狂详解之开源库

youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配置:https://github.com/spf13/spf13-vim ----------------Mac完整项目---------- 电台:https://github.com/myoula/sostart ----------------iOS完整项目---------------- 1,

iOS疯狂详解之视频播放MPMoviePlayerViewController

需要导入的框架 #import <MediaPlayer/MediaPlayer.h> MediaPlayer.framework MPMoviePlayerViewController: 打开网络视频: -(void)openmovie { MPMoviePlayerViewController *movie = [[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLURLWithString:@"视频网络地址&qu

iOS疯狂详解之CocoaPods本身版本升级

查看CocoaPods版本 $ pod --version 0.34.4 确实需要更新了 命令行更新(安装)步骤 $ sudo gem update --system // 先更新gem,国内需要切换源 $ gem sources --remove https://rubygems.org/ $ gem sources -a http://ruby.taobao.org/ $ gem sources -l \*\*\* CURRENT SOURCES \*\*\* http://ruby.tao

iOS疯狂详解之录制音频转换成Mp3

使用第三方 lame ,Mp3音频编码器. 使用 AVAudioRecorder 进行音频录制之前,进行参数设置: NSString *recordTemporaryPathString = [NSString stringWithFormat:@"%@/temporary",self.audioTemporarySavePath]; //LinearPCM 是iOS的一种无损编码格式,但是体积较为庞大 //录音设置 NSMutableDictionary *recordSetting

iOS疯狂详解之AFNetworking图片缓存问题

AFNetworking网络库已经提供了很好的图片缓存机制,效率是比较高的,但是我发现没有直接提供清除缓存的功能,可项目通常都需要添加 清除功能的功能,因此,在这里我以UIImageView+AFNetworking类中添加了下面一个清除功能方法: + (void)clearCache; + (void)clearCache { AFImageCache *cache = (AFImageCache *)[UIImageView sharedImageCache]; [cache removeA

iOS疯狂详解之tableview编辑添加删除

// //  VkoWLAccountVC.m //  PocketUniversity // //  Created by long on 15-1-14. //  Copyright (c) 2015年 WLong. All rights reserved. // #import "VkoWLAccountVC.h" #import "VkoWLMoreTableViewCell.h" #define kIcoArray @[@"消息",@&

iOS疯狂详解之warning:performSelector may cause a leak because its selector is unknown

主要是警告信息,在非ARC项目中没有这个警告.如果是在某一处修改只需要加入下列代码: #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" [self.ticketTarget performSelector: self.ticketAction withObject: self];//此处是你调用函数的地方 #pragma clang diagnosti