你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS瘋狂詳解之ASIHttpRequest的簡單封裝

iOS瘋狂詳解之ASIHttpRequest的簡單封裝

編輯:IOS開發綜合
// iOS瘋狂詳解之ASIHttpRequest的簡單封裝
// WLHTTPClient.h
// WLWLListen
//
// Created by long on 14-12-15.
// Copyright (c) 2014年 WLong. All rights reserved.
//

#import
#import ASIFormDataRequest.h

@protocol WLHTTPClientDelegate
@optional
@end

@interface WLHTTPClient : NSObject

@property (nonatomic,assign) iddelegate;

+ (WLHTTPClient *)sharedClient;


- (void)postInfoWithDelegate:(id)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)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];


}

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved