136 lines
6.7 KiB
Objective-C
Executable File
136 lines
6.7 KiB
Objective-C
Executable File
//
|
||
// NetworkingTool.h
|
||
// yunbaolive
|
||
//
|
||
// Created by 阴天遮住侧脸 on 2020/6/11.
|
||
// Copyright © 2020 cat. All rights reserved.
|
||
//
|
||
|
||
#import <Foundation/Foundation.h>
|
||
#import <AFNetworking/AFNetworking.h>
|
||
|
||
|
||
NS_ASSUME_NONNULL_BEGIN
|
||
|
||
/**网络状态通知Key*/
|
||
//未知网络
|
||
#define NetworkStatusChangKey_Unknown @"NetworkStatusChangKey_Unknown"
|
||
//没有网络
|
||
#define NetworkStatusChangKey_NotReachable @"NetworkStatusChangKey_NotReachable"
|
||
//蜂窝
|
||
#define NetworkStatusChangKey_ReachableViaWWAN @"NetworkStatusChangKey_ReachableViaWWAN"
|
||
//WiFi
|
||
#define NetworkStatusChangKey_ReachableViaWiFi @"NetworkStatusChangKey_ReachableViaWiFi"
|
||
|
||
///请求序列化格式
|
||
typedef NS_ENUM(NSUInteger, CYHRequestSerializer) {
|
||
/// 设置请求数据为JSON格式
|
||
CYHRequestSerializerJSON,
|
||
/// 设置请求数据为二进制格式
|
||
CYHRequestSerializerHTTP,
|
||
};
|
||
///响应序列化格式
|
||
typedef NS_ENUM(NSUInteger, CYHResponseSerializer) {
|
||
/// 设置响应数据为JSON格式
|
||
CYHResponseSerializerJSON,
|
||
/// 设置响应数据为二进制格式
|
||
CYHResponseSerializerHTTP,
|
||
};
|
||
|
||
|
||
#pragma mark - 网络Tool
|
||
@interface NetworkingTool : NSObject
|
||
|
||
#pragma mark - 单例
|
||
//+ (instancetype)shared;
|
||
|
||
/// 会话
|
||
//@property(nonatomic, strong, readonly)AFHTTPSessionManager *sessionManager;
|
||
+ (AFHTTPSessionManager *)sessionManager;
|
||
|
||
#pragma mark - Set_Serializer
|
||
///设置-请求格式
|
||
+ (void)setRequestSerializer:(CYHRequestSerializer)requestSerializer;
|
||
///设置 - 响应格式
|
||
+ (void)setResponseSerializer:(CYHResponseSerializer)responseSerializer;
|
||
|
||
#pragma mark - Set_Timeout
|
||
+ (void)setRequestTimeout:(NSTimeInterval)interval;
|
||
|
||
#pragma mark - Set_SecurityPolicy安全策略
|
||
+ (void)setSecurityPolicy:(AFSecurityPolicy *)securityPolicy;
|
||
|
||
|
||
#pragma mark - POST
|
||
/**
|
||
* POST请求
|
||
* @param host 域名
|
||
* @param hostPath 请求路径
|
||
* ⚠️host与hostPath无需拼接 " / "符号
|
||
* @code
|
||
* url: host/hostPath
|
||
* @endcode
|
||
*/
|
||
+ (nullable NSURLSessionDataTask *)BasePOST_Host:(NSString *)host hostPath:(NSString *)hostPath parameters:(nullable id)paramDic headers:(nullable NSDictionary <NSString *, NSString *> *)headerDic progress:(nullable void (^)(NSProgress *progress))progressBlock success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))successBlock failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failureBlock;
|
||
|
||
/// POST请求(创建并运行一个带有 "POST "请求的 "NSURLSessionDataTask"。)
|
||
/// @param URLString 完整URLString
|
||
/// @param paramDic 参数
|
||
/// @param headerDic 附加到这个请求的默认头部的头部
|
||
/// @param progressBlock 进度回调
|
||
/// @param successBlock 成功回调
|
||
/// @param failureBlock 失败回调
|
||
+ (nullable NSURLSessionDataTask *)BasePOST:(NSString *)URLString parameters:(nullable id)paramDic headers:(nullable NSDictionary <NSString *, NSString *> *)headerDic progress:(nullable void (^)(NSProgress *progress))progressBlock success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))successBlock failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failureBlock;
|
||
|
||
#pragma mark - GET
|
||
/**
|
||
* GET请求
|
||
* @param host 域名
|
||
* @param hostPath 请求路径
|
||
* ⚠️host与hostPath无需拼接 " / "符号
|
||
* @code
|
||
* url: host/hostPath
|
||
* @endcode
|
||
*/
|
||
+ (nullable NSURLSessionDataTask *)BaseGet_Host:(NSString *)host hostPath:(NSString *)hostPath parameters:(NSDictionary *)paramDic headers:(nullable NSDictionary<NSString *,NSString *> *)headerDic progress:(nullable void (^)(NSProgress *progress))progressBlock success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))successBlock failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failureBlock;
|
||
|
||
/// GET请求
|
||
/// @param URLString URLString
|
||
/// @param paramDic 参数
|
||
/// @param headerDic 附加到这个请求的默认头部的头部
|
||
/// @param progressBlock 进度回调
|
||
/// @param successBlock 成功回调
|
||
/// @param failureBlock 失败回调
|
||
+ (nullable NSURLSessionDataTask *)BaseGet:(NSString *)URLString parameters:(NSDictionary *)paramDic headers:(nullable NSDictionary<NSString *,NSString *> *)headerDic progress:(nullable void (^)(NSProgress *progress))progressBlock success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))successBlock failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failureBlock;
|
||
|
||
#pragma mark - 下载
|
||
+ (nullable NSURLSessionDownloadTask *)downloadFileWithURL:(NSString *)urlString saveFilePath:(NSString *)savePath progress:(void (^)(NSProgress *downloadProgress))downloadProgressBlock completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler;
|
||
|
||
#pragma mark - 上传
|
||
|
||
/// upload上传文件
|
||
/// @param filePath 文件路径
|
||
/// @param data 文件二进制数据
|
||
/// @param uploadProgress 上传进度回调
|
||
/// @param completionHandler 上传完成回调
|
||
+ (nullable NSURLSessionUploadTask *)upload:(NSString *)filePath fileData:(NSData *)data progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler;
|
||
|
||
|
||
/// 上传数据 - POST请求 - Form表单提交数据(创建并运行一个包含多部分 "POST "请求的 "NSURLSessionDataTask"。)
|
||
/// @param URLString URLString
|
||
/// @param paramDic 参数
|
||
/// @param headerDic 附加到这个请求的默认头部的头部
|
||
/// @param block 一个接受单个参数并将数据附加到HTTP body的块。block参数是一个采用`AFMultipartFormData`协议的对象。
|
||
/// @param uploadProgress 上传进度回调
|
||
/// @param successBlock 成功回调 ---> id
|
||
/// @param failureBlock 失败回调
|
||
+ (nullable NSURLSessionDataTask *)upload_BasePOST:(NSString *)URLString parameters:(nullable id)paramDic headers:(nullable NSDictionary <NSString *, NSString *> *)headerDic constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))successBlock failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failureBlock;
|
||
|
||
#pragma mark - 网络状态全局监听
|
||
/// 网络状态全局监听, 内部会发送状态改变通知!
|
||
+ (void)setNetStatusObserver;
|
||
|
||
@end
|
||
|
||
NS_ASSUME_NONNULL_END
|