-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Blazor Hybrid WebView not able to Obtain GPS Location #4768
Comments
Note to whoever investigates: There might be some WebView-specific settings to enable code inside the WebView (JavaScript world) to use certain browser APIs. Or maybe there are specific Android app permissions to enable it (not just the regular "does this APP use location", but perhaps a more specific "does this app's WEBVIEW use location". I know we've seen similar things before to do with mic/camera or something like that. |
I do see that it does require some additional code, but no sure how to write for the current platform. |
@jfversluis I tried options mentioned in #3694, but it did not help me in this latest release of MAUI that i am using. |
Would you be able to put this together in a small sample project? Preferable on GitHub? |
Hi @nssidhu. 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. |
Here is the GitRepo |
So i updated the repo to check if native GPS/Location would work and it does. so now in the repo i have native code that is working, but at the same time JavaScript/webview which is not working. I would have ended up using native code and forget about obtaining location from JavaScript for Location, but native code does not have live tracking, i would have to get it via timer. I also noticed that getting location via native code is extremely slow as well. As suggested by @Eilon there might be permission that need to be enabled for WebView/BlazorWebView since that is browser-based permission. Now when I use the same code in Blazor wasm project, i don't need to specify the permission upfront, when JavaScript Code (that will get gps coordinate) is about to execute it prompts user for permission. So, wondering if it is allowed to provide those permission upfront in the code, like we do in native code. |
Hoping this might be helpful to someone who is looking into fixing it within BlazorWebView |
With the link you added earlier, I added this code to your //This is need for enabling webView GPD & Camera Permission.
//If you are not using WebView or using webview but don't need acces to GPS & Camera, this may not be needed at all
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<IBlazorWebView, MauiBlazorWebViewHandler>();
// Gerald: This was added
BlazorWebViewHandler.BlazorWebViewMapper.AppendToMapping("EnableGeoLocation", (handler, webview) =>
{
handler.NativeView.Settings.SetGeolocationEnabled(true);
handler.NativeView.Settings.JavaScriptEnabled = true;
handler.NativeView.Settings.AllowFileAccess = true;
handler.NativeView.Settings.SetGeolocationEnabled(true);
handler.NativeView.Settings.SetGeolocationDatabasePath(handler.NativeView.Context.FilesDir.Path);
});
}); Not 100% sure if these are all needed, but at least this makes it work :) Additionally I noticed a Google Maps API key in there... You might want to invalidate that one now ;) |
@jfversluis Is it your opinion that this is a bug in BlazorWebView, or is a bug in the underlying MAUI WebView, or is not a bug at all and is something we should have covered in docs somewhere? |
Hi @nssidhu. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. 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. |
@jfversluis, regarding key, yes I will invalidate in few days (that one was created a specially for this testing), Thanks for pointing that out though. |
@SteveSandersonMS I would say either something we need to cover in docs (probably more general: this is how to handle the web permissions in .NET MAUI Blazor world) or something we need to make easier for our users. From a user standpoint I would be in favor of basically just enabling and allowing all permissions on the web level since it will be handled on the platform level. On the other hand, we are dealing with sensitive sensors and APIs here, I can also see that people wouldn't appreciate us just opening up everything. |
@nssidhu did you try to reinstall the application? Maybe you wrongfully accidentally denied the platform permission at some point? Could you double-check that? As you can see it works for me :) |
My thoughts on this,
|
I tried uninstalling and re-installing and also checked in the emulator's setting/location that app does have location permission enabled. But still got the error. |
So i have tried it again, still getting permission denied error. One thing i noticed is that during first run, i already got the "permission denied error", even before i got the chance to enable permission in the prompt as you can see in the screen shot below. After providing permission and re-running it , as you can see the app now does have the permission. @jfversluis i am clueless here as to why it worked for you. I even tried moving builder.Services.AddBlazorWebView(); before and after the handler to see if that would make any difference, but it did not |
I think I see the same happening... Here's how I make it work: first deploy, I see the dialog for permission and accept. The error shows. Now, make sure that you do have a location set in the emulator settings and run the app again either through debugging or not. It will now load and show the location although it still says "User denied the request for Geolocation". I didn't inspect the code too much, but I expect there to be something in your code that doesn't handle this properly as it does work on the second try. |
@jfversluis My bad, I should have removed the native code before publishing. I removed/commented it and checked back my code. |
Any updates on how to get this working ? |
@MackinnonBuck can you please include a note in our docs on how to enable location access for WebView? Thanks! |
Please refer to this project as an example for implementing permission management in MAUI Blazor apps for Windows, Android, and iOS. It shows how to access the device's location, camera, and microphone from JavaScript web APIs. We've decided to refer developers to this sample rather than add a docs page since implementing this is rather involved. In the future, we hope to provide a framework-integrated solution that doesn't require individual apps to worry about configuring WebView options and handling permission requests. |
Description
I have created RCL Project which uses GoogleMap. This is purely done in Javascript/HTM/css using google maps Javascript SDK.
I am trying to use this RCL in Blazor Hybrid Project.
It does load the Map, but Location is denied.
Not sure if this was needed but i also entered the permission for android platform in AndroidManifest.xml, that did not help either.
Is there anything else that i need to do?
My location obtaining scripts are in JavaScript file (googleMaps.js) which is my custom JavaScript code that obtains location
and plots the marker for current location.
Additionally, it will also plot multiple marker for nearby business location.
In addition, I Manually gave location permission in the Android emulator, even that did not help
Steps to Reproduce
The steps to create are long.
Version with bug
Preview 13 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
android
Did you find any workaround?
No
Relevant log output
The text was updated successfully, but these errors were encountered: