Skip to content

Commit 8646a46

Browse files
vijaysingh-axwayssekhri
and
ssekhri
committed
fix(ios): server is receiving two consecutive calls for the same url and cookies updated while reloading webview (#11427)
* fix(ios): server is receiving two consecutive calls for the same url * fix(ios): cookies updated while reloading webview Co-authored-by: ssekhri <ssekhri@axway.com>
1 parent a32f713 commit 8646a46

File tree

4 files changed

+109
-19
lines changed

4 files changed

+109
-19
lines changed

iphone/Classes/TiUIWebView.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727
UIActivityIndicatorView *_loadingIndicator;
2828
BOOL _isViewDetached;
2929
BOOL _tiCookieHandlerAdded;
30+
BOOL ignoreNextRequest;
31+
SEL reloadMethod;
3032
}
3133

34+
@property (nonatomic, retain) id reloadData;
35+
3236
// Used from the proxy
3337
- (void)setHtml_:(id)args;
3438
- (void)viewDidClose;
35-
39+
- (void)reload;
3640
- (WKWebView *)webView;
3741

3842
- (void)fireEvent:(id)listenerwithObject:(id)objremove:(BOOL)ynthisObject:(id)thisObject_;

iphone/Classes/TiUIWebView.m

+36-17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ - (void)dealloc
4444
{
4545
RELEASE_TO_NIL(_pageToken);
4646
RELEASE_TO_NIL(_loadingIndicator);
47+
RELEASE_TO_NIL(self.reloadData);
4748
[superdealloc];
4849
}
4950

@@ -174,6 +175,10 @@ - (void)setWillHandleTouches_:(id)value
174175

175176
- (void)setUrl_:(id)value
176177
{
178+
ignoreNextRequest = YES;
179+
self.reloadData = value;
180+
reloadMethod = @selector(setUrl_:);
181+
177182
ENSURE_TYPE(value, NSString);
178183
[[selfproxy] replaceValue:value forKey:@"url"notification:NO];
179184

@@ -203,6 +208,10 @@ - (void)setBackgroundColor_:(id)value
203208

204209
- (void)setData_:(id)value
205210
{
211+
ignoreNextRequest = YES;
212+
self.reloadData = value;
213+
reloadMethod = @selector(setData_:);
214+
206215
[[selfproxy] replaceValue:value forKey:@"data"notification:NO];
207216

208217
if ([[selfwebView] isLoading]) {
@@ -243,6 +252,10 @@ - (void)setBlacklistedURLs_:(id)blacklistedURLs
243252

244253
- (void)setHtml_:(id)args
245254
{
255+
ignoreNextRequest = YES;
256+
self.reloadData = args;
257+
reloadMethod = @selector(setHtml_:);
258+
246259
NSString *content = nil;
247260
NSDictionary *options = nil;
248261

@@ -351,6 +364,18 @@ - (void)setKeyboardDisplayRequiresUserAction_:(id)value
351364
[[selfproxy] replaceValue:value forKey:@"keyboardDisplayRequiresUserAction"notification:NO];
352365
}
353366

367+
- (void)reload
368+
{
369+
if (_webView == nil) {
370+
return;
371+
}
372+
if (self.reloadData != nil) {
373+
[selfperformSelector:reloadMethod withObject:self.reloadData];
374+
return;
375+
}
376+
[[selfwebView] reload];
377+
}
378+
354379
#pragma mark Utilities
355380

356381
- (void)loadRequestWithURL:(NSURL *)url
@@ -894,6 +919,7 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat
894919
if ([[selfproxy] _hasListeners:@"load"]) {
895920
[[selfproxy] fireEvent:@"load"withObject:@{ @"url" : webView.URL.absoluteString, @"title" : webView.title }];
896921
}
922+
ignoreNextRequest = NO;
897923
}
898924

899925
- (void)webView:(WKWebView *)webViewdidFailNavigation:(WKNavigation *)navigationwithError:(NSError *)error
@@ -1058,27 +1084,20 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK
10581084
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
10591085
decisionHandler(WKNavigationActionPolicyCancel);
10601086
} else {
1087+
BOOL valid = !ignoreNextRequest;
1088+
if ([scheme hasPrefix:@"http"]) {
1089+
//UIWebViewNavigationTypeOther means we are either in a META redirect
1090+
//or it is a js request from within the page
1091+
valid = valid && (navigationAction.navigationType != WKNavigationTypeOther);
1092+
}
1093+
if (valid) {
1094+
self.reloadData = navigationAction.request.URL.absoluteString;
1095+
reloadMethod = @selector(setUrl_:);
1096+
}
10611097
decisionHandler(WKNavigationActionPolicyAllow);
10621098
}
10631099
}
10641100

1065-
- (void)webView:(WKWebView *)webViewdecidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponsedecisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
1066-
{
1067-
NSDictionary<NSString *, id> *requestHeaders = [[selfproxy] valueForKey:@"requestHeaders"];
1068-
NSURL *requestedURL = navigationResponse.response.URL;
1069-
1070-
// If we have request headers set, we do a little hack to persist them across different URL's,
1071-
// which is not officially supported by iOS.
1072-
if (requestHeaders != nil && requestedURL != nil && ![requestedURL.absoluteString isEqualToString:_currentURL.absoluteString]) {
1073-
_currentURL = requestedURL;
1074-
decisionHandler(WKNavigationResponsePolicyCancel);
1075-
[selfloadRequestWithURL:_currentURL];
1076-
return;
1077-
}
1078-
1079-
decisionHandler(WKNavigationResponsePolicyAllow);
1080-
}
1081-
10821101
- (WKWebView *)webView:(WKWebView *)webViewcreateWebViewWithConfiguration:(WKWebViewConfiguration *)configurationforNavigationAction:(WKNavigationAction *)navigationActionwindowFeatures:(WKWindowFeatures *)windowFeatures
10831102
{
10841103
if (!navigationAction.targetFrame.isMainFrame) {

iphone/Classes/TiUIWebViewProxy.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ - (void)stopLoading:(id)unused
385385

386386
- (void)reload:(id)unused
387387
{
388-
[[[selfwebView] webView] reload];
388+
TiThreadPerformOnMainThread(
389+
^{
390+
[[selfwebView] reload];
391+
},
392+
NO);
389393
}
390394

391395
- (void)repaint:(id)unused
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Appcelerator Titanium Mobile
3+
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
4+
* Licensed under the terms of the Apache Public License
5+
* Please see the LICENSE included with this distribution for details.
6+
*/
7+
/* eslint-env mocha */
8+
/* eslint no-unused-expressions: "off" */
9+
'use strict';
10+
11+
describe('Titanium.UI.WebView',function(){
12+
varwin;
13+
this.slow(3000);
14+
this.timeout(30000);
15+
16+
afterEach(function(done){
17+
if(win){
18+
// If `win` is already closed, we're done.
19+
lett=setTimeout(function(){
20+
if(win){
21+
win=null;
22+
done();
23+
}
24+
},3000);
25+
26+
win.addEventListener('close',functionlistener(){
27+
clearTimeout(t);
28+
29+
if(win){
30+
win.removeEventListener('close',listener);
31+
}
32+
win=null;
33+
done();
34+
});
35+
win.close();
36+
}else{
37+
win=null;
38+
done();
39+
}
40+
});
41+
42+
it('requestHeaders with redirecting url should work properly',function(finish){
43+
letwebView;
44+
consturl='https://jira.appcelerator.org/';
45+
46+
win=Ti.UI.createWindow();
47+
webView=Ti.UI.createWebView({
48+
url: url,
49+
requestHeaders: {'Custom-field1': 'value1'}
50+
});
51+
52+
webView.addEventListener('load',function(){
53+
finish();
54+
});
55+
56+
webView.addEventListener('error',function(e){
57+
finish(e);
58+
});
59+
60+
win.add(webView);
61+
win.open();
62+
});
63+
});

0 commit comments

Comments
 (0)
close