Press ESC to close

Opening Settings With Xamarin Forms

Hello friends, in this article, we will talk about how to open settings with Xamarin.Forms. In many applications we get data from the device hardware or from its gallery. In order for us to receive this data, the user needs permission. If the user does not allow this permission for the moment, we cannot bring the same permission screen to him again. Of course this is for iOS. On Android, we can ask the user again, but when asking, it gives the option to never ask again. If the user clicks on it, he has to go to the settings for Android and turn on the permission.

As mobile application developers, we must provide convenience to the user. In an operation that requires permission, we should raise a warning if the user has not given permission. In this warning, we should inform the user and then ask him if he wants to go to the settings. If the user wants, we have to go directly to the settings of the application. We used to be able to do this with the Dependency Service. If you want to learn about Dependency Service and how to use it, you can find it here.

In this example, we want to get the location of the user and if the user does not give permission, we will talk about how to send it to the settings. You can find a detailed article about how to get location permission here. With Xamarin.Essentials, this has become very easy. You can open the settings section of the application using the ShowSettingsUI function. More information about Xamarin.Essentials can be found here.

async void GetLocation()
        {
            try
            {
                var location = await Geolocation.GetLastKnownLocationAsync();
                if (location != null)
                {
                    Latitude = location.Latitude.ToString();
                    Longitude = location.Longitude.ToString();
                }
            }
            catch (FeatureNotSupportedException ex)
            {
                await App.Current.MainPage.DisplayAlert(AppResources.warning, AppResources.locationFeatureNotSupportedError, AppResources.ok);
                ResetLocation();
            }
            catch (FeatureNotEnabledException ex)
            {
                await App.Current.MainPage.DisplayAlert(AppResources.warning, AppResources.locationFeatureNotEnabledError, AppResources.ok);
                ResetLocation();
            }
            catch (PermissionException ex)
            {
                var result = await App.Current.MainPage.DisplayAlert(AppResources.warning, AppResources.locationPermissionError, AppResources.ok, AppResources.cancel);
                if (result)
                    AppInfo.ShowSettingsUI();
                ResetLocation();
            }
            catch (Exception ex)
            {
                await App.Current.MainPage.DisplayAlert(AppResources.warning, AppResources.generalError, AppResources.ok);
                ResetLocation();
            }
        }

The screen output of the application is as follows.

If you have questions, you can reach us by sending an e-mail or comment. Good work.

Leave a Reply

Your email address will not be published. Required fields are marked *