From fc1133732d1d33405e0cf693a74e61d5ef7783a3 Mon Sep 17 00:00:00 2001 From: Vijay Vikram Singh Date: Thu, 9 Jan 2020 15:28:50 -0800 Subject: [PATCH] fix(ios): cookies updated while reloading webview (#11415) * fix(ios): cookies updated in reloading webview * fix(ios): cookies updated while reloading webview Co-authored-by: ssekhri --- iphone/Classes/TiUIWebView.h | 6 +++++- iphone/Classes/TiUIWebView.m | 36 +++++++++++++++++++++++++++++++ iphone/Classes/TiUIWebViewProxy.m | 6 +++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/iphone/Classes/TiUIWebView.h b/iphone/Classes/TiUIWebView.h index b99accb5ddd..df422a69d65 100644 --- a/iphone/Classes/TiUIWebView.h +++ b/iphone/Classes/TiUIWebView.h @@ -27,12 +27,16 @@ UIActivityIndicatorView *_loadingIndicator; BOOL _isViewDetached; BOOL _tiCookieHandlerAdded; + BOOL ignoreNextRequest; + SEL reloadMethod; } +@property (nonatomic, retain) id reloadData; + // Used from the proxy - (void)setHtml_:(id)args; - (void)viewDidClose; - +- (void)reload; - (WKWebView *)webView; - (void)fireEvent:(id)listener withObject:(id)obj remove:(BOOL)yn thisObject:(id)thisObject_; diff --git a/iphone/Classes/TiUIWebView.m b/iphone/Classes/TiUIWebView.m index bcb8c43d089..1dc8d0c4d1d 100644 --- a/iphone/Classes/TiUIWebView.m +++ b/iphone/Classes/TiUIWebView.m @@ -44,6 +44,7 @@ - (void)dealloc { RELEASE_TO_NIL(_pageToken); RELEASE_TO_NIL(_loadingIndicator); + RELEASE_TO_NIL(self.reloadData); [super dealloc]; } @@ -172,6 +173,10 @@ - (void)setWillHandleTouches_:(id)value - (void)setUrl_:(id)value { + ignoreNextRequest = YES; + self.reloadData = value; + reloadMethod = @selector(setUrl_:); + ENSURE_TYPE(value, NSString); [[self proxy] replaceValue:value forKey:@"url" notification:NO]; @@ -201,6 +206,10 @@ - (void)setBackgroundColor_:(id)value - (void)setData_:(id)value { + ignoreNextRequest = YES; + self.reloadData = value; + reloadMethod = @selector(setData_:); + [[self proxy] replaceValue:value forKey:@"data" notification:NO]; if ([[self webView] isLoading]) { @@ -241,6 +250,10 @@ - (void)setBlacklistedURLs_:(id)blacklistedURLs - (void)setHtml_:(id)args { + ignoreNextRequest = YES; + self.reloadData = args; + reloadMethod = @selector(setHtml_:); + NSString *content = nil; NSDictionary *options = nil; @@ -349,6 +362,18 @@ - (void)setKeyboardDisplayRequiresUserAction_:(id)value [[self proxy] replaceValue:value forKey:@"keyboardDisplayRequiresUserAction" notification:NO]; } +- (void)reload +{ + if (_webView == nil) { + return; + } + if (self.reloadData != nil) { + [self performSelector:reloadMethod withObject:self.reloadData]; + return; + } + [[self webView] reload]; +} + #pragma mark Utilities - (void)loadRequestWithURL:(NSURL *)url @@ -892,6 +917,7 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat if ([[self proxy] _hasListeners:@"load"]) { [[self proxy] fireEvent:@"load" withObject:@{ @"url" : webView.URL.absoluteString, @"title" : webView.title }]; } + ignoreNextRequest = NO; } - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error @@ -1063,6 +1089,16 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK [[UIApplication sharedApplication] openURL:navigationAction.request.URL]; decisionHandler(WKNavigationActionPolicyCancel); } else { + BOOL valid = !ignoreNextRequest; + if ([scheme hasPrefix:@"http"]) { + //UIWebViewNavigationTypeOther means we are either in a META redirect + //or it is a js request from within the page + valid = valid && (navigationAction.navigationType != WKNavigationTypeOther); + } + if (valid) { + self.reloadData = navigationAction.request.URL.absoluteString; + reloadMethod = @selector(setUrl_:); + } decisionHandler(WKNavigationActionPolicyAllow); } } diff --git a/iphone/Classes/TiUIWebViewProxy.m b/iphone/Classes/TiUIWebViewProxy.m index fa16f38d91a..86109a6b467 100644 --- a/iphone/Classes/TiUIWebViewProxy.m +++ b/iphone/Classes/TiUIWebViewProxy.m @@ -385,7 +385,11 @@ - (void)stopLoading:(id)unused - (void)reload:(id)unused { - [[[self webView] webView] reload]; + TiThreadPerformOnMainThread( + ^{ + [[self webView] reload]; + }, + NO); } - (void)repaint:(id)unused