I’m developing a .NET MAUI app and currently testing it on Windows.
In my code, I’m attempting to access the device's location using the browser's location API. When the app requests location access, a popup appears, allowing the user to either allow or deny the permission. For testing purposes, I denied the location permission, and the API returned an error indicating that the location permission was rejected. I’ve implemented functionality to display this error message on the screen.
I’m unsure where the rejection is stored. Every time I run the app on my computer, the location popup no longer appears. Where can I go to reset the location settings so that the popup will show up again?
async function getBrowserLocation()
{
return new Promise((resolve, reject) => {
if (!navigator.geolocation)
{
reject(JSON.stringify({ Code: UNKNOWN_ERROR, Message: "Geolocation is not supported by this browser." }));
} else
{
navigator.geolocation.getCurrentPosition(
(position) => {
resolve(position);
},
(error) => {
var message = "", code = "";
switch (error.code)
{
case error.PERMISSION_DENIED:
message = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
message = "Location information is unavailable."
break;
case error.TIMEOUT:
message = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
message = "An unknown error occurred."
break;
}
reject(JSON.stringify({ Code: error.code, Message: message }));
}
);
}
});
}
I’m developing a .NET MAUI app and currently testing it on Windows.
In my code, I’m attempting to access the device's location using the browser's location API. When the app requests location access, a popup appears, allowing the user to either allow or deny the permission. For testing purposes, I denied the location permission, and the API returned an error indicating that the location permission was rejected. I’ve implemented functionality to display this error message on the screen.
I’m unsure where the rejection is stored. Every time I run the app on my computer, the location popup no longer appears. Where can I go to reset the location settings so that the popup will show up again?
async function getBrowserLocation()
{
return new Promise((resolve, reject) => {
if (!navigator.geolocation)
{
reject(JSON.stringify({ Code: UNKNOWN_ERROR, Message: "Geolocation is not supported by this browser." }));
} else
{
navigator.geolocation.getCurrentPosition(
(position) => {
resolve(position);
},
(error) => {
var message = "", code = "";
switch (error.code)
{
case error.PERMISSION_DENIED:
message = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
message = "Location information is unavailable."
break;
case error.TIMEOUT:
message = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
message = "An unknown error occurred."
break;
}
reject(JSON.stringify({ Code: error.code, Message: message }));
}
);
}
});
}
This is how I trigger the javascript function
public async Task<Geolocation> GetBrowserLocation()
{
Geolocation geolocation = null;
try
{
var module = await moduleTask.Value;
GeolocationPosition geolocationPosition = await module.InvokeAsync<GeolocationPosition>("getBrowserLocation");
geolocation = new Geolocation() { GeolocationPosition = geolocationPosition };
return geolocation;
}
catch (Exception ex)
{
geolocation = JsonSerializer.Deserialize<Geolocation>(ex.Message);
}
return geolocation;
}
Share
Improve this question
edited Mar 27 at 18:46
Ali
asked Mar 23 at 18:50
AliAli
1,2551 gold badge19 silver badges46 bronze badges
7
- Can you share the code that you've written that asks for the initial permission. – tonyedwardspz Commented Mar 25 at 11:57
- When the page appears, you could check the permission status. If the status is no, asking for the initial permission again. – WenyanZhang Commented Mar 26 at 3:01
- JavaScript code added. It ask user for the permissions and If denied, it does not show popup again. In chrome and other browsers I can go to setting and reset location settings so user can be asked for permissions again but don't know how to do that in Windows App. – Ali Commented Mar 26 at 3:20
- How did you call the js? On which method? Try to get the location permission status by MAUI api: PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>(); – WenyanZhang Commented Mar 27 at 3:44
- I have updated the code, possibly I should not be calling javascript location in windows app?? Windows app should be treated like Android/iOS app and use Xamarin native API to get location?? – Ali Commented Mar 27 at 18:47
1 Answer
Reset to default 1Every time I run the app on my computer, the location popup no longer appears.
It's the default behavior on WinUI app (MAUI targeted on Windows uses WinUI).
After the user denied the permission, the popup won't appear again. For this, you could show a prompt to let the user jump to the Settings page to allow location.
For example
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
PermissionStatus permissionStatus;
private async Task IncrementCount()
{
currentCount++;
permissionStatus = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();// you could also use the js code to check the permission
if (permissionStatus == PermissionStatus.Denied)
{
await Launcher.OpenAsync("ms-settings:privacy-location");//open settings page
}
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744272755a4566181.html
评论列表(0条)