NetWorkHelper.h
#import <Foundation/Foundation.h>
@protocol NetWorkHelperDelegate <NSObject>
@required
//參數(shù)為所需要傳出去的值(解析好的)
- (void)passValueWithData: (id)value;
@end
@interface NetWorkHelper : NSObject
@property (nonatomic,assign) id<NetWorkHelperDelegate> delegate;
//同步get請求
- (void)getAndSynchronousMethodWithURL: (NSString *)urlString;
//同步post請求
- (void)postAndSynchronousMethodWithURL: (NSString *)urlString;
//異步post請求
- (void)postAndAsynchronousMethodWithUrl: (NSString *)urlString;
//異步get
- (void)getAndAsynchronousMethodWithURL: (NSString *)urlString;
@end
NetWorkHelper.m
#import "NetWorkHelper.h"
@implementation NetWorkHelper
- (void)jsonParserWithData: (NSData *)data
{
if (data) {
id value = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
[self.delegate passValueWithData:value];
}
}
//同步get請求
- (void)getAndSynchronousMethodWithURL: (NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error;
NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
[self jsonParserWithData:receiveData];
}
//異步POST請求
- (void)postAndAsynchronousMethodWithUrl: (NSString *)urlString
{
NSArray *array = [urlString componentsSeparatedByString:@"?"];
NSURL *url = [NSURL URLWithString:array[0]];
NSData *postData = [array[1] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//設(shè)置請求方式
request.HTTPMethod = @"POST";
//設(shè)置請求參數(shù)
request.HTTPBody = postData;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
[self jsonParserWithData:data];
}];
}
//同步post
- (void)postAndSynchronousMethodWithURL: (NSString *)urlString
{
NSArray *array = [urlString componentsSeparatedByString:@"?"];
NSURL *url = [NSURL URLWithString:array[0]];
//創(chuàng)建POST請求參數(shù)恬汁,為NSData類型
NSString *postString = array[1];
//將string類型轉(zhuǎn)換為NSData類型
NSData *postParameterData = [postString dataUsingEncoding:NSUTF8StringEncoding];
//創(chuàng)建請求弯囊,因為NSURLRequest類型不能設(shè)置請求方式,所以如果是post請求,就得使用它的子類NSMutableURLRequest
NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:url];
//設(shè)置請求方式
mutableReq.HTTPMethod = @"POST";
//設(shè)置請求參數(shù)
mutableReq.HTTPBody = postParameterData;
//建立同步連接
NSData *receiveData = [NSURLConnection sendSynchronousRequest:mutableReq returningResponse:nil error:nil];
[self jsonParserWithData:receiveData];
}
//異步get
- (void)getAndAsynchronousMethodWithURL: (NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
[self jsonParserWithData:data];
}];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者