-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MIGRATED: iOS WebView doesn't support hash url change. The onNavigationStateChange listener will not be called when url hash changes. (#20447) #24
Comments
make compatible with phoebe-mobile in RN version 0.57
Should it change? It should definitely be consistent between Android and iOS, but should it change when the URL hash changes? The URL hash typically refers to some portion of the document; clicking an anchor link will fast forward through the document to that portion. But is the behavior really any different from scrolling? Scrolling also moves the viewport to a different portion of the document. I think if you look at most Router implementations, currently, they will actually change the URL (not just the hash) when any significant change in the UI occurs. Then again I could imagine multi-step forms which use the hash to indicate at which portion of the form a user resides; should we support that? Does onNavigationStateChange trigger any other functionality? Such as calling injectedJavascript? |
I think it would be useful to give more info to the users? I have use cases where I want to monitor when the url changes and I have to use postMessage inside react-router to notify react-native of the change (to update navigation title for example) |
I think there are a lot of single-page applications (via hashes) that change, I think it's useful to be able to listen for hash changes. |
@xupengkun my use case, exactly |
so have u fixed this hash url change state guys? |
Not yet, feel free to send a PR |
I has the same requirement too, I have a WebView which is a single-page application. And |
Its very important for web apps please add this feature 😭 Please. |
Here is a solution that seems to work for me on iOS: Inject JS onto the page that sets the initial url and then continuously checks the current url against the initial one. If the user has navigated to a new page, the url will be different. When that happens you can send a postMessage back to your React Native code to handle the change and then set the current url to be checked again. Example:
As an injectable template string, this would look like:
Now, in your onMessage function you can listen for this object being sent back and then handle it however you need to.
Some notes:
|
I was able to get this working pretty cleanly by monkey patching the <Webview
injectedJavaScript={`
(function() {
function wrap(fn) {
return function wrapper() {
var res = fn.apply(this, arguments);
window.ReactNativeWebView.postMessage('navigationStateChange');
return res;
}
}
history.pushState = wrap(history.pushState);
history.replaceState = wrap(history.replaceState);
window.addEventListener('popstate', function() {
window.ReactNativeWebView.postMessage('navigationStateChange');
});
})();
true;
`}
onMessage={({ nativeEvent: state }) => {
if (state.data === 'navigationStateChange') {
// Navigation state updated, can check state.canGoBack, etc.
}
}}
/>; Maybe this should be built in? |
@janicduplessis Am I missing something? It doesn't look like you can check |
@adamrainsby state is |
Interesting @janicduplessis! We might want to add this to the docs |
I added it to the docs ^^ |
@janicduplessis workaround works perfectly on iOS, but unfortunately not on Android, where the nativeEvent only has the "data" property like stated in the docs. It only has all the other event properties on iOS. |
This issue seems to also occur whenever |
Waiting for a fix on android =\ Our website is made in Vue (SPA) |
Is the workaround also needed on Android? I haven't tested it yet. |
state.canGoBack == undefined |
I actually submitted a pull request, #641, to fix this in the WebView itself. The pull request doesn't interfere with the I closed the pull request because it didn't look like it would work, but perhaps the I am dedicated to getting a proper in-tree solution to this problem. I am also willing to consider other API's as well. It seems like people would also like access to the |
@janicduplessis it works in iOS, but not works in Android... need help please :( |
Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically |
Silly bot, there has been an open pull request on this issue for months! So, yes, the community is trying to help fix issues, but the maintainers don't seem interested in accepting help. |
@swansontec it is really sad :( |
Here is a solution for grabbing the current URL that works on iOS and Android. It's an adaptation of the code from @janicduplessis above. Again, focused on passing back current URL only, not the other bits normally received in onNavigationStateChange.
As the docs for onMessage say, whatever string you pass into |
@timraybould I just tried what you have suggested, but seems to only detect the first time the navigation page changes, but not for times after that. Any suggestions? |
@timraybould this works perfectly for me on Android! Thank you! 👏 |
See react-native-webview#24 andhttps://github.com/react-native-webview/pull/1082/files
Very perfect. And we can post window.history.length.
Webview onMessage
|
Migrated from facebook/react-native#20447.
@ll929 says:
Environment
React Native Environment Info:
System:
OS: macOS High Sierra 10.13.3
CPU: x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
Memory: 109.62 MB / 8.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 9.11.1 - ~/.nvm/versions/node/v9.11.1/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 5.6.0 - ~/.nvm/versions/node/v9.11.1/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 11.2, macOS 10.13, tvOS 11.2, watchOS 4.2
Android SDK:
Build Tools: 23.0.1, 23.0.2, 23.0.3, 24.0.1, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.3
API Levels: 23, 24, 25, 26, 27
IDEs:
Android Studio: 3.1 AI-173.4819257
Xcode: 9.2/9C40b - /usr/bin/xcodebuild
npmPackages:
react: 16.4.1 => 16.4.1
react-native: 0.56.0 => 0.56.0
Description
Describe your issue in detail. Include screenshots if needed. If this is a regression, let us know.
Reproducible Demo
Let us know how to reproduce the issue. Include a code sample, share a project, or share an app that reproduces the issue using https://snack.expo.io/. Please follow the guidelines for providing a MCVE: https://stackoverflow.com/help/mcve
Expected Behavior
When url hash change, the webivew onNavigationStateChange listener will be invoked, and the canGoBack properties is true
Actual Behavior
When url hash changes, the webivew onNavigationStateChange hasn't been invoked
The text was updated successfully, but these errors were encountered: