Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 240159f

Browse files
committed
fix($browser/$location): add a workaround of iOS9 regression in UIWebView
This works around: https://openradar.appspot.com/22186109 Closes #12241 Related to ionic-team/ionic-framework#4082
1 parent 00ee090 commit 240159f

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

src/ng/browser.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,45 @@ function Browser(window, document, $log, $sniffer) {
350350
function $BrowserProvider() {
351351
this.$get = ['$window', '$log', '$sniffer', '$document',
352352
function($window, $log, $sniffer, $document) {
353-
return new Browser($window, $document, $log, $sniffer);
353+
var browser = new Browser($window, $document, $log, $sniffer);
354+
355+
if (isIOS9UIWebView($window.navigator.userAgent)) {
356+
browser = applyIOS9Shim(browser);
357+
}
358+
359+
return browser;
354360
}];
355361
}
362+
363+
364+
function isIOS9UIWebView(userAgent) {
365+
return /(iPhone|iPad|iPod) OS 9_\d/.test(userAgent) && !/Version\/9\./.test(userAgent);
366+
}
367+
368+
369+
function applyIOS9Shim(browser) {
370+
var pendingLocationUrl = null;
371+
var patchedBrowser = Object.create(browser);
372+
373+
patchedBrowser.url = function() {
374+
if (arguments.length) {
375+
pendingLocationUrl = arguments[0];
376+
377+
// reset the current pendingLocationUrl in the next task
378+
(function() {
379+
var pendingLocationUrlToReset = pendingLocationUrl;
380+
setTimeout(function() {
381+
if (pendingLocationUrlToReset === pendingLocationUrl) {
382+
pendingLocationUrl = null;
383+
}
384+
},0);
385+
}());
386+
387+
return browser.url.apply(patchedBrowser, arguments);
388+
}
389+
390+
return pendingLocationUrl || browser.url();
391+
};
392+
393+
return patchedBrowser;
394+
}

test/ng/browserSpecs.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
/* global getHash:true, stripHash:true */
3+
/* global getHash:true, stripHash:true, isIOS9UIWebView:true */
44

55
var historyEntriesLength;
66
var sniffer = {};
@@ -852,4 +852,21 @@ describe('browser', function() {
852852

853853
});
854854

855+
856+
describe('iOS9 UIWebView workarounds', function() {
857+
858+
describe('userAgent sniffing', function() {
859+
860+
it('should identify iOS9 + UIWebView', function() {
861+
var userAgentString = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13A4325c (358330912)';
862+
expect(isIOS9UIWebView(userAgentString)).toBe(true);
863+
});
864+
865+
it('should ignore iOS9 + WKWebView', function() {
866+
var userAgentString = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13A4325c Safari/601.1';
867+
expect(isIOS9UIWebView(userAgentString)).toBe(false);
868+
});
869+
});
870+
});
871+
855872
});

test/ng/rafSpec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ describe('$$rAF', function() {
8282
location: window.location,
8383
history: window.history,
8484
webkitRequestAnimationFrame: jasmine.createSpy('$window.webkitRequestAnimationFrame'),
85-
webkitCancelRequestAnimationFrame: jasmine.createSpy('$window.webkitCancelRequestAnimationFrame')
85+
webkitCancelRequestAnimationFrame: jasmine.createSpy('$window.webkitCancelRequestAnimationFrame'),
86+
navigator: {
87+
userAgent: 'mock navigator'
88+
}
8689
});
8790
}]);
8891

0 commit comments

Comments
 (0)