Pxaml
Pxaml

Reputation: 553

How to open the app privacy settings from xamarin forms

Trying to open my app settings , so the user can see the permissions my app is required. cant find anything with a similar example. *

if (item.Name == "Privacy preferences")
            {
                switch (Device.RuntimePlatform)
                {
                    case Device.iOS:
                        Device.OpenUri(
                          new Uri(FORGOT WHAT TO PUT IN HERE .. APP/SETTINGS?);
                        break;
                    case Device.Android:
                        Device.OpenUri(
                          new Uri();
                        break;
                }

*

Upvotes: 3

Views: 3598

Answers (2)

Johannes
Johannes

Reputation: 958

Try Device.OpenUri(new Uri("app-settings:"));

If this doesn't work (I think it has a while ago), you probably have to do this in your platform specific parts and use e.g. the dependency service. For IOS then use UIApplication.SharedApplication.OpenUrl(new NSUrl("app-settings:"));

Edit: @EvZ's answer is the one with platform specific code and the abstraction, if you also need UWP you can call await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-location"));

Upvotes: 2

EvZ
EvZ

Reputation: 12179

Fairly simple, you will have to create platform specific implementations.

The Interface

public interface ISettingsHelper
{
    void OpenAppSettings();
}

iOS

public void OpenAppSettings()
{
    var url = new NSUrl($"app-settings:{app_bundle_id}");
    UIApplication.SharedApplication.OpenUrl(url);
}

Android

public void OpenAppSettings()
{
    var intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
    intent.AddFlags(ActivityFlags.NewTask);
    var uri = Android.Net.Uri.FromParts("package", package_name, null);
    intent.SetData(uri);
    Application.Context.StartActivity(intent);
}

From Xamarin.Forms project you could simple call OpenAppSettings();.

P.S.: Please keep in mind that this solution requires tweaking if you would like it to work on older devices.

Upvotes: 11

Related Questions