forked from illusionspaces/WKJavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWKWebViewCookieMgr.m
More file actions
executable file
·58 lines (50 loc) · 1.91 KB
/
WKWebViewCookieMgr.m
File metadata and controls
executable file
·58 lines (50 loc) · 1.91 KB
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
//
// WKWebViewCookieMgr.m
// Hybrid-framework
//
// Created by Kevin on 2019/4/4.
// Copyright © 2019 王凯. All rights reserved.
//
#import "WKWebViewCookieMgr.h"
@implementation WKWebViewCookieMgr
+ (void)syncRequestCookie:(NSMutableURLRequest *)request {
if (!request.URL) {
return;
}
NSArray *availableCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies;
if (availableCookie.count > 0) {
NSDictionary *reqheader = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookie];
NSString *cookieStr = [reqheader objectForKey:@"Cookie"];
[request setValue:cookieStr forHTTPHeaderField:@"Cookie"];
}
return;
}
+ (NSString *)clientCookieScripts {
NSArray *availableCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies;
NSMutableArray *filterCookie = [[NSMutableArray alloc]init];
for (NSHTTPCookie * cookie in availableCookie) {
if (!cookie.HTTPOnly) {
[filterCookie addObject:cookie];
}
}
if (filterCookie.count > 0) {
for (NSHTTPCookie *cookie in filterCookie) {
NSTimeInterval expiretime = [cookie.expiresDate timeIntervalSince1970];
NSString *js = [NSString stringWithFormat:@"document.cookie ='%@=%@;expires=%f';",cookie.name,cookie.value,expiretime];
return js;
}
}
return nil;
}
+ (NSMutableURLRequest *)newRequest:(NSURLRequest *)request {
NSMutableURLRequest *newReq = [request mutableCopy];
NSMutableArray *array = [NSMutableArray array];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL]) {
NSString *value = [NSString stringWithFormat:@"%@=%@", cookie.name, cookie.value];
[array addObject:value];
}
NSString *cookie = [array componentsJoinedByString:@";"];
[newReq setValue:cookie forHTTPHeaderField:@"Cookie"];
return newReq;
}
@end