-
Notifications
You must be signed in to change notification settings - Fork 0
/
YYAFHTTPSessionManager.m
84 lines (71 loc) · 3.24 KB
/
YYAFHTTPSessionManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// YYAFHTTPSessionManager.m
// YYUniversalApp
//
// Created by 杨世擎 on 2019/9/10.
// Copyright © 2019 YSQ. All rights reserved.
//
#import "YYAFHTTPSessionManager.h"
@implementation YYAFHTTPSessionManager
+ (instancetype)HTTPSessionManagerWithRequestThreadType:(HTTPRequestThreadType)type
{
if (type == HTTPRequestThreadType_Main)
{
return [self HTTPSessionManager_NoBaseURL_Main];
}
else if (type == HTTPRequestThreadType_Child)
{
return [self HTTPSessionManager_NoBaseURL_ChildThread];
}
return [self HTTPSessionManager_NoBaseURL_Main];
}
+ (instancetype)HTTPSessionManager_NoBaseURL_Main
{
static YYAFHTTPSessionManager *HTTPSessionManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
HTTPSessionManager = [[YYAFHTTPSessionManager alloc] initWithSessionConfiguration:config];
[self setRequestHeader:HTTPSessionManager];
// [self setHttpsCer:HTTPSessionManager];
});
return HTTPSessionManager;
}
//默认回到子线程
+ (instancetype)HTTPSessionManager_NoBaseURL_ChildThread
{
static YYAFHTTPSessionManager *HTTPSessionManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
HTTPSessionManager = [[YYAFHTTPSessionManager alloc] initWithSessionConfiguration:config];
HTTPSessionManager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
HTTPSessionManager.operationQueue.maxConcurrentOperationCount = 3;
[self setRequestHeader:HTTPSessionManager];
});
return HTTPSessionManager;
}
// 设置请求头 默认
+ (void)setRequestHeader:(YYAFHTTPSessionManager *)tools
{
tools.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain",nil];
[tools.requestSerializer willChangeValueForKey:@"timeoutInterval"];
tools.requestSerializer.timeoutInterval = 20.f;
[tools.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[tools.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[tools.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; // 此处设置content-Type生效了,
NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
NSString *currentBundleId = infoDict[@"CFBundleIdentifier"];
if (currentBundleId) {
[tools.requestSerializer setValue:currentBundleId forHTTPHeaderField:@"pkgName"];
}
[tools.requestSerializer setValue:SoftwareVersion forHTTPHeaderField:@"versionName"];//app版本
[tools.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
[tools.requestSerializer setValue:KSID forHTTPHeaderField:@"appId"];
}
+ (void)cancelAFNetWork {
// [self->tasks makeObjectsPerformSelector:@selector(cancel)];
static YYAFHTTPSessionManager *HTTPSessionManager;
[HTTPSessionManager.tasks makeObjectsPerformSelector:@selector(cancelAll)];
}
@end