apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 987 forks source link

Routing error in WKWebView on iOS16 and above when building with Xcode14.3 #1324

Closed tanukki2929 closed 9 months ago

tanukki2929 commented 1 year ago

Bug Report

Problem

What is expected to happen?

What does actually happen?

Routing error on iOS16 and above when building Angular app with Xcode14.3

ERROR Error: Uncaught (in promise): SecurityError: Blocked attempt to use history.pushState() to change session history URL from file:///Users/appname/Library/Developer/CoreSimulator/Devices/zzzzzzzz-yyyy-dddd-cccccc-bbbbbbbb/data/Containers/Bundle/Application/xxxxxxxx-yyyy-zzzz-wwww-aaaaaaaaaaaaa/APPNAME.app/index.html/ to file:///Users/appname/Library/Developer/CoreSimulator/Devices/zzzzzzzz-yyyy-dddd-cccccc-bbbbbbbb/data/Containers/Bundle/Application/xxxxxxxx-yyyy-zzzz-wwww-aaaaaaaaaaaaa/APPNAME.app/index.html/dashboard. Only differences in query and fragment are allowed for file: URLs.

The same problem reported in the APPLE forum below occurs. Please let us know if there is any way to solve this problem with Cordova. https://developer.apple.com/forums/thread/728157

Information

Command or Code

Environment, Platform, Device

Version information

Xcode バージョン 14.3 (14E222b) "cordova-ios": "^6.3.0"

Checklist

dpogue commented 1 year ago

The short answer is that you'll need to use a custom scheme instead of file URLs.

The long answer is that what you're trying to do doesn't make sense with file URLs and is actually forbidden by the HTML specification: https://html.spec.whatwg.org/multipage/history.html#can-have-its-url-rewritten

  1. If targetURL's scheme is "file", and targetURL and documentURL differ in their path component, then return false. (Differences in query and fragment are allowed for file: URLs.)

WebKit changed their behaviour in iOS 16.4 to match Chrome and the HTML spec: https://github.com/WebKit/WebKit/commit/78a55cf3976ce0b4543df42b03c335c5b89d27dd


So it's fine to use pushState with file:/// URLs to go from index.html to index.html#/dashboard, but it is not allowed to go from index.html to index.html/dashboard.

Maybe your framework has an option to use hash-based URLs for routing?

tanukki2929 commented 1 year ago

Thank you for your response. I appreciate it very much. We will see if we can address the issue based on your response.

msmtamburro commented 11 months ago

The short answer is that you'll need to use a custom scheme instead of file URLs.

Hi @dpogue. Is the scheme option currently broken? I noticed this today: https://github.com/apache/cordova-ios/issues/1223#issuecomment-1608211672

jcesarmobile commented 11 months ago

It's not broken, I've just created a new app with cordova-ios 6.3.0 and it's working fine with a custom scheme.

msmtamburro commented 11 months ago

@jcesarmobile it seems broken when the URL contains a query or a fragment, for instance. In this case, the resulting URL will be blank.

jcesarmobile commented 11 months ago

In that case, can you create a new issue and provide a sample app that reproduces that issue?

But for regular use case, it's working fine.

AlexBorsody commented 11 months ago

Running a Cordova app and ran across this same error in Xcode. I added this to my config.xml to set a custom URL scheme and it fixed the issue for me.

<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />
sathishavunoori commented 9 months ago

Hi, We are also having the same issue with our Cordova iOS app, our app is working on iOS 15 devices but when we run the same app on iOS 16.4 devices, the navigation from www/index.html to www/pages/ui/page1.html using $.mobile.changePage API is not working. Can any one suggest us how to fix this problem in cordova ios app with cordova-ios:6.3.0 or 7.0.0 versions with target SDK 16+

breautek commented 9 months ago

The original reporter reported this issue in the context of using the history API with file:// protocol and answer on that subject is: https://github.com/apache/cordova-ios/issues/1324#issuecomment-1542206012

Hi, We are also having the same issue with our Cordova iOS app, our app is working on iOS 15 devices but when we run the same app on iOS 16.4 devices, the navigation from www/index.html to www/pages/ui/page1.html using $.mobile.changePage API is not working. Can any one suggest us how to fix this problem in cordova ios app with cordova-ios:6.3.0 or 7.0.0 versions with target SDK 16+

As noted above, if $.mobile.changePage is using the history API, then you either need to use schemes (see https://github.com/apache/cordova-ios/issues/1324#issuecomment-1613704842) or you need to use a hash-based routing. Without knowing what $.mobile.changePage actually does we can't provide further insights.

Closing as not-a-bug.