Skip to content

Commit

Permalink
fix(ios): server is receiving two consecutive calls for the same url …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
vijaysingh-axway and ssekhri committed Jan 9, 2020
1 parent a32f713 commit 8646a46
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 19 deletions.
6 changes: 5 additions & 1 deletion iphone/Classes/TiUIWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
53 changes: 36 additions & 17 deletions iphone/Classes/TiUIWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (void)dealloc
{
RELEASE_TO_NIL(_pageToken);
RELEASE_TO_NIL(_loadingIndicator);
RELEASE_TO_NIL(self.reloadData);
[super dealloc];
}

Expand Down Expand Up @@ -174,6 +175,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];

Expand Down Expand Up @@ -203,6 +208,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]) {
Expand Down Expand Up @@ -243,6 +252,10 @@ - (void)setBlacklistedURLs_:(id)blacklistedURLs

- (void)setHtml_:(id)args
{
ignoreNextRequest = YES;
self.reloadData = args;
reloadMethod = @selector(setHtml_:);

NSString *content = nil;
NSDictionary *options = nil;

Expand Down Expand Up @@ -351,6 +364,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
Expand Down Expand Up @@ -894,6 +919,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
Expand Down Expand Up @@ -1058,27 +1084,20 @@ - (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);
}
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
NSDictionary<NSString *, id> *requestHeaders = [[self proxy] valueForKey:@"requestHeaders"];
NSURL *requestedURL = navigationResponse.response.URL;

// If we have request headers set, we do a little hack to persist them across different URL's,
// which is not officially supported by iOS.
if (requestHeaders != nil && requestedURL != nil && ![requestedURL.absoluteString isEqualToString:_currentURL.absoluteString]) {
_currentURL = requestedURL;
decisionHandler(WKNavigationResponsePolicyCancel);
[self loadRequestWithURL:_currentURL];
return;
}

decisionHandler(WKNavigationResponsePolicyAllow);
}

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
Expand Down
6 changes: 5 additions & 1 deletion iphone/Classes/TiUIWebViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions tests/Resources/ti.ui.webview.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';

describe('Titanium.UI.WebView', function () {
var win;
this.slow(3000);
this.timeout(30000);

afterEach(function (done) {
if (win) {
// If `win` is already closed, we're done.
let t = setTimeout(function () {
if (win) {
win = null;
done();
}
}, 3000);

win.addEventListener('close', function listener () {
clearTimeout(t);

if (win) {
win.removeEventListener('close', listener);
}
win = null;
done();
});
win.close();
} else {
win = null;
done();
}
});

it('requestHeaders with redirecting url should work properly', function (finish) {
let webView;
const url = 'https://jira.appcelerator.org/';

win = Ti.UI.createWindow();
webView = Ti.UI.createWebView({
url: url,
requestHeaders: { 'Custom-field1': 'value1' }
});

webView.addEventListener('load', function () {
finish();
});

webView.addEventListener('error', function (e) {
finish(e);
});

win.add(webView);
win.open();
});
});

0 comments on commit 8646a46

Please sign in to comment.