码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-12-16 19:20:12      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:ios开发   asihttprequest   ios   

//  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

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

标签:ios开发   asihttprequest   ios   

原文地址:http://blog.csdn.net/wanglongblog/article/details/41963191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!