Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5397d5

Browse files
committed
1.fix cookie missing problems with WKWebView2.fix no alert panel problems
1 parent 70b24f9 commit a5397d5

File tree

3 files changed

+92
-23
lines changed

3 files changed

+92
-23
lines changed

SmartJSWebView.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@
482482
buildSettings = {
483483
CODE_SIGN_IDENTITY = "";
484484
CODE_SIGN_STYLE = Automatic;
485-
CURRENT_PROJECT_VERSION = 202005150001;
485+
CURRENT_PROJECT_VERSION = 202005210001;
486486
DEFINES_MODULE = YES;
487487
DYLIB_COMPATIBILITY_VERSION = 1;
488488
DYLIB_CURRENT_VERSION = 1;
@@ -491,7 +491,7 @@
491491
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
492492
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
493493
MACH_O_TYPE = staticlib;
494-
MARKETING_VERSION = 2.0.4;
494+
MARKETING_VERSION = 2.0.5;
495495
OTHER_CFLAGS = "-fembed-bitcode";
496496
OTHER_LDFLAGS = (
497497
"-lObjC",
@@ -509,7 +509,7 @@
509509
buildSettings = {
510510
CODE_SIGN_IDENTITY = "";
511511
CODE_SIGN_STYLE = Automatic;
512-
CURRENT_PROJECT_VERSION = 202005150001;
512+
CURRENT_PROJECT_VERSION = 202005210001;
513513
DEFINES_MODULE = YES;
514514
DYLIB_COMPATIBILITY_VERSION = 1;
515515
DYLIB_CURRENT_VERSION = 1;
@@ -518,7 +518,7 @@
518518
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
519519
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
520520
MACH_O_TYPE = staticlib;
521-
MARKETING_VERSION = 2.0.4;
521+
MARKETING_VERSION = 2.0.5;
522522
OTHER_CFLAGS = "-fembed-bitcode";
523523
OTHER_LDFLAGS = (
524524
"-lObjC",
@@ -537,12 +537,12 @@
537537
CODE_SIGN_IDENTITY = "-";
538538
CODE_SIGN_STYLE = Automatic;
539539
COMBINE_HIDPI_IMAGES = YES;
540-
CURRENT_PROJECT_VERSION = 202005150001;
540+
CURRENT_PROJECT_VERSION = 202005210001;
541541
ENABLE_BITCODE = NO;
542542
INFOPLIST_FILE = SmartJSWebViewResource/Info.plist;
543543
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Bundles";
544544
MACOSX_DEPLOYMENT_TARGET = 10.13;
545-
MARKETING_VERSION = 2.0.4;
545+
MARKETING_VERSION = 2.0.5;
546546
PRODUCT_BUNDLE_IDENTIFIER = com.lessney.SmartJSWebViewResource;
547547
PRODUCT_NAME = SmartJSWebView;
548548
SDKROOT = iphoneos;
@@ -557,12 +557,12 @@
557557
CODE_SIGN_IDENTITY = "-";
558558
CODE_SIGN_STYLE = Automatic;
559559
COMBINE_HIDPI_IMAGES = YES;
560-
CURRENT_PROJECT_VERSION = 202005150001;
560+
CURRENT_PROJECT_VERSION = 202005210001;
561561
ENABLE_BITCODE = NO;
562562
INFOPLIST_FILE = SmartJSWebViewResource/Info.plist;
563563
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Bundles";
564564
MACOSX_DEPLOYMENT_TARGET = 10.13;
565-
MARKETING_VERSION = 2.0.4;
565+
MARKETING_VERSION = 2.0.5;
566566
PRODUCT_BUNDLE_IDENTIFIER = com.lessney.SmartJSWebViewResource;
567567
PRODUCT_NAME = SmartJSWebView;
568568
SDKROOT = iphoneos;

SmartJSWebView/public_headers/SmartJSWebView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// 框架名称:SmartJSWebView
99
// 框架功能:支持 H5 页面通过 JavaScript 与 Native App 交互的 WebView。
1010
// 修改记录:
11+
// pcjbird 2020-05-21 Version:2.0.5 Build:202005210001
12+
// 1.fix cookie missing problems with WKWebView
13+
// 2.fix no alert panel problems
14+
//
1115
// pcjbird 2020-05-15 Version:2.0.4 Build:202005150001
1216
// 1.bug fixed
1317
//

SmartJSWebView/source/SmartJSWebViewProxy.m

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@
1818
static const float SmartJSWebViewProgressInteractiveValue = 0.9f;
1919
static const float SmartJSWebViewProgressFinalProgressValue = 0.9f;
2020

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+
2150
@interface SmartJSWebViewProxy()
2251
{
2352
int _loadingCount;
@@ -323,6 +352,7 @@ - (void)userContentController:(WKUserContentController *)userContentController d
323352

324353
#pragma mark - WKNavigationDelegate
325354
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
355+
NSMutableDictionary<NSString *, NSString *> *requestHeaders = [navigationAction.request.allHTTPHeaderFields mutableCopy];
326356

327357
NSMutableURLRequest *fixedRequest = [navigationAction.request isKindOfClass:[NSMutableURLRequest class]] ? (NSMutableURLRequest *)(navigationAction.request) : [navigationAction.request mutableCopy];
328358

@@ -331,28 +361,40 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
331361
NSString* host = fixedRequest.URL.host;
332362
if([host isKindOfClass:[NSString class]] && [host length] > 0)
333363
{
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) {
337369
if([host rangeOfString:obj.domain].location != NSNotFound)
338370
{
339-
[resolvedCookies addObject:obj];
371+
[requestCookies addObject:obj];
372+
[script appendFormat:@"if (cookieNames.indexOf('%@') == -1) { document.cookie='%@'; };\n", obj.name, obj.kc_formatCookieString];
340373
}
341374
}];
342375

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) {
345379
WKHTTPCookieStore *cookieStore = [WKWebsiteDataStore defaultDataStore].httpCookieStore;
346380
[cookieStore setCookie:obj completionHandler:nil];
347381
}];
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+
}
356398
}
357399
}
358400

@@ -505,7 +547,30 @@ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSStrin
505547
[self.realDelegate webView:webView runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame completionHandler:completionHandler];
506548
return;
507549
}
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];
509574
}
510575

511576
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler

0 commit comments

Comments
 (0)