dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.08k stars 1.73k forks source link

[ios] HtmlWebViewSource.BaseUrl is applied with an unexpected prefix #19792

Open dodikk opened 9 months ago

dodikk commented 9 months ago

Description

Setting HtmlWebViewSource.BaseUrl results in different window.location.href on iOS and Android respectively.


ACTUAL RESULT (from debug alert() javascript logs)

Android:

Screenshot 2024-01-09 at 19 56 24

iOS:

Screenshot 2024-01-09 at 19 58 09

EXPECTED RESULT window.location.href is equal to HtmlWebViewSource.BaseUrl on both platforms

Steps to Reproduce

private static string BaseUrlForCaptcha => "https://identity-dev.coinpaymints.com";

// then in ctor() of ContentView
{
    PlainWebviewHtmlSource.BaseUrl = BaseUrlForCaptcha;
    PlainWebviewHtmlSource.Html = pageContentsHtml;
}

// then in onload() in pageContentsHtml
alert(window.location.href);

Link to public reproduction project repository

https://github.com/dodikk/bug-maui-webview-baseurl

Version with bug

7.0.101

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No workaround found

So an important sign-up (aka "register") flow of my app is broken.

Relevant log output

No response

ghost commented 9 months ago

Hi @dodikk. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

dodikk commented 9 months ago

Here is a minimal sample link as requested https://github.com/dodikk/bug-maui-webview-baseurl

dodikk commented 9 months ago

The native WKWebView of iOS sdk shows the baseUrl as expected. So it seems like MAUI is modifying it under the hood for some reason. So please add a flag to disable that behaviour when needed.

Screenshot 2024-01-10 at 15 10 31

[upd] same results for the UIWebView native control

p.s. the native sample is in the same repository https://github.com/dodikk/bug-maui-webview-baseurl

dodikk commented 9 months ago

Seems like NSBundle.MainBundle.BundlePath is concatenated with the value provided by me.

However, i am unable to find that in maui codebase https://github.com/search?q=repo%3Adotnet%2Fmaui+%22NSBundle.MainBundle.BundlePath%22&type=code

ghost commented 9 months ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

XamlTest commented 8 months ago

Verified this on Visual Studio Enterprise 17.9.0 Preview 5(8.0.3). Repro on iOS 17.2 and MacCatalyst with below Project: BaseUrlBugDemo.zip

IsvAdvanced commented 6 months ago

Something of sorts happening for us in iOS: in our case we load local file.

We set BaseUrl like this:

image

And it does not work properly because we get a Trailing Slash added in the output. Output of window.location.href:

file:///private/var/containers/Bundle/Application/-App Guid-/-Package Unique Name-/Content/index.html/ (note that slash after html)

I've done a test to get rid of that extra slash and then the page works:

image

I don't know why that Trailing Slash is being added automatically. The following code works fine (for Android):

image

P.S.: If anyone landed here and was trying to load local files, this will do:

image

image

dodikk commented 6 months ago

@IsvAdvanced thanks for your info. The trailing slash does happen for me as well (as you can see on the attached screenshots). However, that is not breaking my case.

dodikk commented 6 months ago

@IsvAdvanced Unfortunately that does not answer my question since I have a slightly different situation. What I am trying to achieve is

  1. opening a local file in webView
  2. while having the baseUrl of the website front-end
  3. in order to let the user interact with re-captcha

That does indeed work for me on

  1. on android.
  2. with native WKWebView of iOS (both swiftlang and C#)

So this ticket has been created for MS team to implement the same behavior for MAUI WebView.

dodikk commented 6 months ago

@IsvAdvanced if you want that trailing slash fixed by MS team, it might be a good idea to create a separate ticket (like this one). Separate ticket seems to make sense since that slash is a different problem.

p.s. it is appended by native WKWebView, so it seems like little can be done to fix that. So you might be forced to stick with your workaround unfortunately.

IsvAdvanced commented 6 months ago

@IsvAdvanced if you want that trailing slash fixed by MS team, it might be a good idea to create a separate ticket (like this one). Separate ticket seems to make sense since that slash is a different problem.

p.s. it is appended by native WKWebView, so it seems like little can be done to fix that. So you might be forced to stick with your workaround unfortunately.

The issue could very well be the same: I think the point for us both is that what we set in BaseUrl should be respected.

We got this behaviour after migrating to MAUI from Xamarin (there it worked fine).

dodikk commented 6 months ago

I think the point for us both is that what we set in BaseUrl should be respected.

@IsvAdvanced well, yes and no. I view these as two special cases of the same "should be respected" ideal. Different symptoms, different root causes, etc.

dodikk commented 6 months ago

We got this behaviour after migrating to MAUI from Xamarin (there it worked fine).

@IsvAdvanced "trailing slash working fine under x.forms" - might be due to x.forms using a deprecated UIWebView rather than WKWebView or newer iOS webView controls.

p.s. thanks for boosting this issue activity. No much action from MS unfortunately.

smeagol75 commented 3 months ago

As a Workaround we finally solved this issue with a custom handler and a custom view, the problem is here where the local Path is added image

We have added a custom handler on IOS with a custom view and removed the dir constructor image and the custom view Overriding the OpenHtml method image Hope this helps

dodikk commented 3 months ago

@smeagol75 thanks for the workaround. If that's a change not within maui framework but rather in my sample code - could you please send a PullRequest to that repository? https://github.com/dodikk/bug-maui-webview-baseurl

Or please share a link to your PR if you have already done so. (For some reason i do not see it so far) Thanks in advance.

dodikk commented 3 months ago

@smeagol75 in case your fix and workaround are within maui framework... could you please explain how to update my visualstudio or workflows (to which beta/nightly/etc) so that the changes would take effect on my sample and my main app. Thanks again.

dodikk commented 2 months ago

@smeagol75 so can we please have your code as text rather than merely as screenshots? Thanks again for the reply.

dodikk commented 2 months ago

Ok, i guess, I've managed to apply the workaround and it seems to work ok.

https://github.com/dodikk/bug-maui-webview-baseurl/tree/a73ef05b07c03cb3f52fe992f91e99c89b29d0ea