本身封裝的網絡請求


#import <Foundation/Foundation.h>

//定義枚舉
typedef NS_ENUM(NSInteger){
    LDT_GET,
    LDT_POST
}URLRequestMethod;

//定義返回值block網絡請求結果
typedef void(^ResultBlock)(NSData *data,NSURLResponse *response,NSError *error);


@interface LDTNetWorkRequestManager : NSObject



///請求方法
+ (void)requestURL:(NSURL*)url Method:(URLRequestMethod)method Params:(NSDictionary *)params Result:(ResultBlock)resultBlock;


@end

數組

 


#import "LDTNetWorkRequestManager.h"

@interface LDTNetWorkRequestManager ()

//存儲回調block的屬性
//@property(nonatomic,copy)ResultBlock block;


@end

@implementation LDTNetWorkRequestManager

//請求方法
+ (void)requestURL:(NSURL *)url Method:(URLRequestMethod)method Params:(NSDictionary *)params Result:(ResultBlock)resultBlock
{
    ///空的可變字符串
    NSMutableString *urlString = [NSMutableString new];
    //isKindOfClass來肯定一個對象是不是一個類的成員,或者是派生自該類的成員
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接url
        [urlString appendString:url.absoluteString];
        
    }else if ([url isKindOfClass:[NSString class]]){
        
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length ==0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:3.1415926 userInfo:@{@"reason":@"你輸入的網址不正確"}];
        resultBlock(nil,nil,error);
        return;
    }
    //把參數字典轉換爲字符串
    LDTNetWorkRequestManager *manager = [LDTNetWorkRequestManager new];
    
    NSString *paraStr = [manager strFromDict:params];
    
    switch (method) {
        case LDT_GET:
        {
            if (paraStr.length > 0) {
                
                [urlString appendFormat:@"?%@",paraStr];
            }
            [manager requestURL:[NSURL URLWithString:urlString] ResultBlock:resultBlock];
            
        }
            break;
            
        case LDT_POST:
            [manager postURL:urlString paraStr:paraStr ResultBlock:resultBlock];
            break;
        default:
            break;
    }
    
}

#pragma mark - GET網絡請求

- (void)requestURL:(NSURL *)url ResultBlock:(ResultBlock)block
{
    NSURLSessionDataTask *task = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:block];
                                  
    //開始網絡請求
    [task resume];
}

#pragma mark - POST網絡請求

- (void)postURL:(NSString *)urlStr paraStr:(NSString *)paraStr ResultBlock:(ResultBlock)block
{
    //建立NSURL
    NSURL *url = [NSURL URLWithString:urlStr];
    //建立可變請求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //設置POST方法
    [mRequest setHTTPMethod:@"POST"];
    //請求體
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
    
   
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:block];
                                      
    [dataTask resume];
    
}




- (NSString *)strFromDict:(NSDictionary *)dict
{
    //空數組
    NSMutableArray *array = [NSMutableArray new];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@ = %@",key,dict[key]];
        [array addObject:str];
    }
    //數組拼接字符串
    NSString *para = [array componentsJoinedByString:@"&"];
    NSLog(@"%@",para);
    //返回
    return para;
}




@end

網絡

相關文章
相關標籤/搜索