美文网首页
学习苹果提供的原生网络请求方法

学习苹果提供的原生网络请求方法

作者: yangSirQ | 来源:发表于2020-08-30 10:38 被阅读0次

    一、学习苹果提供的原生网络请求方法

    在iOS项目开发中,网络请求是必不可少的部分,大多数iOS开发者会使用第三方的网络请求框架AFNetworking。AFNetworking基本每个iOS开发者都知道,但是却很少有人去阅读过它的源码。都会用,但却不知道它的原理,可谓是最熟悉的陌生人。要想弄懂AFNetworking的原理,首先要掌握苹果提供的原生网络请求方法,AFNetworking也是在这个基础之上进行二次封装的。本篇文章主要是介绍原生的网络请求方法,内容很简单。

    NSURLSessionDataTask 是 NSURLSessionTask 的子类,是一个具体的网络请求类,是网络请求中最常用的请求之一。NSURLSessionDataTask 用来请求数据,可以用来下载数据资源,例如 JSON 数据,图片数据等。

    1、NSURLSession 的实例方法一

    - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
    
    

    该方法中会自动将 url 转换为一个请求对象(NSURLRequest),并且该请求对象是 GET 请求方式。回调方法是在子线程中运行的,所以如果在回调方法中刷新 UI 必须回到主线程中。使用该方法的缺点是不能监听获取数据的进度,因为只有当全部请求完数据后,才会调用这个方法,也就是说,data 中的数据是请求的全部数据。

    代码示例

    @property (nonatomic, strong) NSURLSessionDataTask * dataTask;
    
    self.urlString = @"https://icweiliimg9.pstatp.com/weili/l/462548014891532564.jpg";
    
    - (void)GETButtonClick
    {
        self.dataTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:self.urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imgView.image = [UIImage imageWithData:data];
            });
        }];
        [self.dataTask resume];
    }
    
    

    2、NSURLSession 的实例方法二

    - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;
    
    

    方法一只能是GET请求方式,方法二可以手动设置请求方式GET或POST。
    代码示例

    - (void)PostButtonClick
    {
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.urlString]];
        request.HTTPMethod = @"POST";
        self.dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imgView.image = [UIImage imageWithData:data];
            });
        }];
        [self.dataTask resume];
    }
    
    

    3、NSURLSession 的代理方法

    方法一和方法二唯一的缺点就是不能监控请求进度,因为只有当请求完全部的数据后才会调用回调方法,如果想要监控请求进度,必须使用代理的方法。

    + (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(nullable id <NSURLSessionDelegate>)delegate delegateQueue:(nullable NSOperationQueue *)queue;
    
    

    代码示例

    - (void)request
    {
        self.urlString = @"https://icweiliimg9.pstatp.com/weili/l/462548014891532564.jpg";
        self.data = [[NSMutableData alloc] init];
        self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
        self.dataTask = [self.session dataTaskWithURL:[NSURL URLWithString:self.urlString]];
        [self.dataTask resume];
    }
    
    
    #pragma mark 接受到数据时调用,可能会调用多次
    - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
    {
        [self.data appendData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"progress:%f",(float)self.dataTask.countOfBytesReceived);
        });
    }
    
    
    #pragma mark 请求结束或失败时调用
    - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imgView.image = [UIImage imageWithData:self.data];
        });
    }
    
    

    二、基于原生网络请求方法进行封装

    掌握了原生的网络请求方法,我们便可以自己封装一个简单的网络请求类,在简单的项目中可以替代AFNetworking,或者可以在自己的SDK中进行使用。下面就直接上代码了,只是对文章前面的内容进行了封装。

    1、HSNetworkTool.h 头文件

    @interface HSNetworkTool : NSObject
    
    + (instancetype)shareInstance;
    
    //请求URL
    @property (nonatomic, copy) NSString *requestURL;
    
    typedef void(^HSResponseSuccessBlock)(NSDictionary *responseObject);
    
    typedef void(^HSResponseFailBlock)(NSError *error);
    
    /**
     * @brief   GET请求方法
     * @author  yuancan
    
     * @param relativePath 接口名称
     * @param params 请求参数
     * @param successBlock 请求成功回调
     * @param failBlock 请求失败回调
     */
    - (void)requestGET:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock;
    
    /**
     * @brief   POST请求方法
     * @author  yuancan
    
     * @param relativePath 接口名称
     * @param params 请求参数
     * @param successBlock 请求成功回调
     * @param failBlock 请求失败回调
     */
    - (void)requestPOST:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock;
    
    /**
     * @brief   JSON格式网络接口请求方法
     * @author  yuancan
     *
     * @param relativePath 接口名称
     * @param params 请求参数
     * @param successBlock 请求成功回调
     * @param failBlock 请求失败回调
     */
    - (void)requestJsonPost:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock;
    
    @end
    
    

    2、HSNetworkTool.m 源文件

    #import "HSNetworkTool.h"
    
    @implementation HSNetworkTool
    
    + (instancetype)shareInstance
    {
        static HSNetworkTool *_sharedInstance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedInstance = [[HSNetworkTool alloc] init];
            [_sharedInstance configure];
        });
        return _sharedInstance;
    }
    
    - (void)configure
    {
        self.requestURL = @"https://wx.ahdms.top";
    }
    
    - (void)requestHTTPMethod:(NSString *)httpMenthod relativePath:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock
    {
    
        NSMutableString *paramsString = [[NSMutableString alloc] initWithCapacity:0];
        for (int i=0;i<[params allKeys].count;i++) {
            NSString *key = [[params allKeys] objectAtIndex:i];
            [paramsString appendString:[NSString stringWithFormat:@"%@=%@",key,[params objectForKey:key]]];
            if (i < [params allKeys].count-1) {
                [paramsString appendString:@"&"];
            }
        }
    
        NSString *urlString = [NSString stringWithFormat:@"%@%@", self.requestURL, relativePath];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
        request.HTTPMethod = httpMenthod;
        request.HTTPBody = [paramsString dataUsingEncoding:NSUTF8StringEncoding];
        NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (!error) {
                if (successBlock) {
                    NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
                    successBlock(responseObject);
                }
            } else {
                if (failBlock) {
                    failBlock(error);
                }
            }
        }];
        [dataTask resume];
    }
    
    - (void)requestGET:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock
    {
        [self requestHTTPMethod:@"GET" relativePath:relativePath params:params successBlock:^(NSDictionary * _Nonnull responseObject) {
            if (successBlock) {
                return successBlock(responseObject);
            }
        } failBlock:^(NSError * _Nonnull error) {
            if (failBlock) {
                return failBlock(error);
            }
        }];
    }
    
    - (void)requestPOST:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock
    {
        [self requestHTTPMethod:@"POST" relativePath:relativePath params:params successBlock:^(NSDictionary * _Nonnull responseObject) {
            if (successBlock) {
                return successBlock(responseObject);
            }
        } failBlock:^(NSError * _Nonnull error) {
            if (failBlock) {
                return failBlock(error);
            }
        }];
    }
    
    - (void)requestJsonPost:(NSString *)relativePath params:(NSDictionary *)params successBlock:(HSResponseSuccessBlock)successBlock failBlock:(HSResponseFailBlock)failBlock
    {
        NSString *urlString = [NSString stringWithFormat:@"%@%@", self.requestURL, relativePath];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
        request.HTTPMethod = @"POST";
        request.HTTPBody = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
        [request addValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
        NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (!error) {
                if (successBlock) {
                    NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
                    successBlock(responseObject);
                }
            } else {
                if (failBlock) {
                    failBlock(error);
                }
            }
        }];
        [dataTask resume];
    }
    
    @end
    
    

    3、使用示例

    GET请求

     [[HSNetworkTool shareInstance] requestGET:@"/login"
                                           params:nil
                                     successBlock:^(NSDictionary * _Nonnull responseObject) {
    
                                     }
                                     failBlock:^(NSError * _Nonnull error) {
    
                                     }];
    
    

    POST请求

    [[HSNetworkTool shareInstance] requestPOST:@"/login"
                                           params:nil
                                     successBlock:^(NSDictionary * _Nonnull responseObject) {
    
                                     }
                                     failBlock:^(NSError * _Nonnull error) {
    
                                     }];
    
    

    JSON格式的POST请求

    [[HSNetworkTool shareInstance] requestJsonPost:@"/login"
                                           params:nil
                                     successBlock:^(NSDictionary * _Nonnull responseObject) {
    
                                     }
                                     failBlock:^(NSError * _Nonnull error) {
    
                                     }];
    

    转载https://blog.csdn.net/u010545480/article/details/100538799

    相关文章

      网友评论

          本文标题:学习苹果提供的原生网络请求方法

          本文链接:https://www.haomeiwen.com/subject/viossktx.html