简体   繁体   中英

Getting access denied for Geolocator on Windows 8

The following code is giving me an access denied exception:

Geolocator geolocator = new Geolocator();
Geoposition geopos = await geolocator.GetGeopositionAsync();

The error is as follows:

A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I have given Location Capability and Declaration.

Can anyone suggest why I may be getting this error?

Did your app prompt you for consent when it started? If so, and you clicked Block, then you'll get this exception.

If you didn't see the prompt, then permissions are cached--go to Settings > Permissions from your app to change it. Or uninstall the app and rerun, which will reset consent (as will changing your manifest and running again).

It's also possible that Location is turned off system wide. Check PC Settings > Privacy > Location (on Windows 8.1; I forget where it is in Windows 8, but should be in there somewhere).

Bottom line, though, is that because a user can deny consent on first access, and later turn it off via Settings > Permissions, you should always catch such exceptions with brokered APIs like this. (That's the pattern: those APIs throw instead of returning a null in such cases.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM