18
18
static const float SmartJSWebViewProgressInteractiveValue = 0 .9f ;
19
19
static const float SmartJSWebViewProgressFinalProgressValue = 0 .9f ;
20
20
21
+ @interface NSHTTPCookie (Util)
22
+
23
+ /* *
24
+ format cookie to string
25
+
26
+ @return cookieStringValue
27
+ */
28
+ - (NSString *)kc_formatCookieString ;
29
+
30
+ @end
31
+
32
+ @implementation NSHTTPCookie (Util)
33
+
34
+ - (NSString *)kc_formatCookieString {
35
+ NSString *string = [NSString stringWithFormat: @" %@ =%@ ;domain=%@ ;path=%@ " ,
36
+ self .name,
37
+ self .value,
38
+ self .domain,
39
+ self .path ?: @" /" ];
40
+
41
+ if (self.secure ) {
42
+ string = [string stringByAppendingString: @" ;secure=true" ];
43
+ }
44
+
45
+ return string;
46
+ }
47
+
48
+ @end
49
+
21
50
@interface SmartJSWebViewProxy ()
22
51
{
23
52
int _loadingCount;
@@ -323,6 +352,7 @@ - (void)userContentController:(WKUserContentController *)userContentController d
323
352
324
353
#pragma mark - WKNavigationDelegate
325
354
- (void )webView : (WKWebView *)webView decidePolicyForNavigationAction : (WKNavigationAction *)navigationAction decisionHandler : (void (^)(WKNavigationActionPolicy ))decisionHandler {
355
+ NSMutableDictionary <NSString *, NSString *> *requestHeaders = [navigationAction.request.allHTTPHeaderFields mutableCopy ];
326
356
327
357
NSMutableURLRequest *fixedRequest = [navigationAction.request isKindOfClass: [NSMutableURLRequest class ]] ? (NSMutableURLRequest *)(navigationAction.request ) : [navigationAction.request mutableCopy ];
328
358
@@ -331,28 +361,40 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
331
361
NSString * host = fixedRequest.URL .host ;
332
362
if ([host isKindOfClass: [NSString class ]] && [host length ] > 0 )
333
363
{
334
- NSMutableArray <NSHTTPCookie *>* resolvedCookies = [NSMutableArray <NSHTTPCookie *> array];
335
- NSArray <NSHTTPCookie *> *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage ].cookies ;
336
- [cookies enumerateObjectsUsingBlock: ^(NSHTTPCookie * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
364
+ NSMutableString *script = [NSMutableString string ];
365
+ [script appendString: @" var cookieNames = document.cookie.split('; ').map(function(cookie) { return cookie.split('=')[0] } );\n " ];
366
+ NSMutableArray <NSHTTPCookie *>* requestCookies = [NSMutableArray <NSHTTPCookie *> array];
367
+ NSArray <NSHTTPCookie *> *localCookies = [NSHTTPCookieStorage sharedHTTPCookieStorage ].cookies ;
368
+ [localCookies enumerateObjectsUsingBlock: ^(NSHTTPCookie * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
337
369
if ([host rangeOfString: obj.domain].location != NSNotFound )
338
370
{
339
- [resolvedCookies addObject: obj];
371
+ [requestCookies addObject: obj];
372
+ [script appendFormat: @" if (cookieNames.indexOf('%@ ') == -1) { document.cookie='%@ '; };\n " , obj.name, obj.kc_formatCookieString];
340
373
}
341
374
}];
342
375
343
- if (@available (iOS 11.0 , *)) {
344
- [resolvedCookies enumerateObjectsUsingBlock: ^(NSHTTPCookie * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
376
+ if (@available (iOS 11.0 , *))
377
+ {
378
+ [requestCookies enumerateObjectsUsingBlock: ^(NSHTTPCookie * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
345
379
WKHTTPCookieStore *cookieStore = [WKWebsiteDataStore defaultDataStore ].httpCookieStore ;
346
380
[cookieStore setCookie: obj completionHandler: nil ];
347
381
}];
348
- }
349
- else {
350
- NSDictionary <NSString *, NSString *> * resolvedSharedHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies: resolvedCookies];
351
- NSMutableDictionary <NSString *, NSString *> *originHeaders = navigationAction.request .allHTTPHeaderFields .mutableCopy ;
352
- [originHeaders setValuesForKeysWithDictionary: resolvedSharedHeaderFields];
353
- fixedRequest.allHTTPHeaderFields = originHeaders;
354
- NSLog (@" originalHeaders:%@ " , originHeaders);
355
- }
382
+ }
383
+ else
384
+ {
385
+ NSDictionary <NSString *, NSString *> * resolvedLocalHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies: requestCookies];
386
+ NSString * resolvedCookieString = [resolvedLocalHeaderFields objectForKey: @" Cookie" ];
387
+ if (resolvedCookieString && (![requestHeaders objectForKey: @" Cookie" ] || [[requestHeaders objectForKey: @" Cookie" ] rangeOfString: resolvedCookieString].location == NSNotFound ))
388
+ {
389
+ [requestHeaders setValuesForKeysWithDictionary: resolvedLocalHeaderFields];
390
+ fixedRequest.allHTTPHeaderFields = requestHeaders;
391
+ WKUserScript * cookieScript = [[WKUserScript alloc ] initWithSource: script injectionTime: WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly: NO ];
392
+ [webView.configuration.userContentController addUserScript: cookieScript];
393
+ [webView loadRequest: fixedRequest];
394
+ decisionHandler (WKNavigationActionPolicyCancel );
395
+ return ;
396
+ }
397
+ }
356
398
}
357
399
}
358
400
@@ -505,7 +547,30 @@ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSStrin
505
547
[self .realDelegate webView: webView runJavaScriptAlertPanelWithMessage: message initiatedByFrame: frame completionHandler: completionHandler];
506
548
return ;
507
549
}
508
- completionHandler ();
550
+ SmartJSWebView *jsWebView = (SmartJSWebView*)[webView superview ];
551
+ if (![jsWebView isKindOfClass: [SmartJSWebView class ]])
552
+ {
553
+ completionHandler ();
554
+ return ;
555
+ }
556
+ UIViewController *viewController = nil ;
557
+ UIResponder *responder = jsWebView;
558
+ while ((responder = [responder nextResponder ])){
559
+ if ([responder isKindOfClass: [UIViewController class ]]){
560
+ viewController = (UIViewController *)responder;
561
+ break ;
562
+ }
563
+ }
564
+ if (!viewController)
565
+ {
566
+ completionHandler ();
567
+ return ;
568
+ }
569
+ UIAlertController *alertController = [UIAlertController alertControllerWithTitle: @" " message: message?:@" " preferredStyle: UIAlertControllerStyleAlert];
570
+ [alertController addAction: ([UIAlertAction actionWithTitle: NSLocalizedString(@" Confirm" , nil ) style: UIAlertActionStyleDefault handler: ^(UIAlertAction * _Nonnull action) {
571
+ completionHandler ();
572
+ }])];
573
+ [viewController presentViewController: alertController animated: YES completion: nil ];
509
574
}
510
575
511
576
- (void )webView : (WKWebView *)webView runJavaScriptConfirmPanelWithMessage : (NSString *)message initiatedByFrame : (WKFrameInfo *)frame completionHandler : (void (^)(BOOL result))completionHandler
0 commit comments