如何在iOS9 UIWebview中修复window.location问题

时间:2015-09-22 14:29:18

标签: uiwebview ios9 window.location hashchange

自iOS9发布以来,我的网络应用程序不再起作用了。 自iOS9以来,应用程序以不可预测的方式使用基于url哈希的导航和路由更改触发器。

我做了很多研究,并在IOS9 UIWebview中管理window.location时遇到了问题。

与这篇文章相关,角度社区似乎已经修复了angular.js的已发布补丁的问题。

https://gist.github.com/IgorMinar/863acd413e3925bf282c

该问题已被确定为: 使用window.location

时,iOS9 UIWebview不会同步更新href

我在IOS9中查看了他们的更正,但我找不到重现其修正的方法,以使其适用于任何(非角度)webapp。

https://github.com/angular/angular.js/commit/8d39bd8abf423517b5bff70137c2a29e32bff76d#otherHash https://github.com//petebacondarwin/angular.js/commit/47f16f35c213dbb165567102231ac1496246c456 https://github.com//angular/angular.js/blob/master/src/ng/browser.js

是否有人找到或正在制定解决方案,使角度解决方案适用于任何(非角度)网络应用?

更新

这是我打电话的链接

domain/player/index.html

然后

domain/player/index.html#/online/presentation/ru1XA0nkvgHm/slide/0

[self.view stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.location.href = '#/offline/presentation/%@/slide/%ld?isNativeApp=1%@%@'",moduleId,(long)slideNumber,infoIcons,testingMode]]);

我做了测试,发现了一些奇怪的东西。

如果我用最后一个命令进行双重调用,第二次,hashchange事件会触发。

我不想将应用程序重新提交到应用程序商店,我想修改我的html(可以从服务器获取)以检测哈希更改并使应用程序正常工作。

2 个答案:

答案 0 :(得分:4)

问题是此特定浏览器在下次运行JavaScript事件循环之前不会更新window.location.href的值。这意味着如果您写入该值然后立即将其读回,则会得到不同的值:

console.log(window.location.href)   // -> http://my.domain.com/path/to/page
window.location.href = 'http://my.domain.com/path/to/other/page';
console.log(window.location.href)   // -> http://my.domain.com/path/to/page

// next tick of the event loop

console.log(window.location.href)   // -> http://my.domain.com/path/to/other/page

请注意,第二个console.log返回旧值,而不是新值。当前事件循环完成后,将更新该值,如第三个console.log中所示。

我们提出的修复方法是缓存我们编写的值,如果浏览器没有同步更新,然后从那时开始使用该值,而不是window.location.href返回的值,直到有一个hashchange事件,告诉我们浏览器最终将自己排序。

以下是AngularJS中提交的链接,其中修复了此问题:https://github.com/angular/angular.js/commit/8d39bd8abf423517b5bff70137c2a29e32bff76d

答案 1 :(得分:3)

window.setTimeout(function(){
      //the second time you update your url.
      updateUrl();
},0);

该错误是因为在IOS 9中,当您更改时,location.href / location.hash不会立即更改:location.href = http://yoururl.com,它将在下一个事件循环中更改。添加window.settimeout将强制代码在下一个Event Loop中执行。