• Open App-Settings in Xamarin Forms

    Sometimes it’s necessary for the user to grant some access rights, e.g. for camera or photo library. Typically the user is asked on first access, but if he denies he will not be prompted anymore. So you should forward the user to the settings page instead of just saying “Hey, go to your settings page and do the changes”.

    For iOS it’s quite easy to do in Xamarin Forms ( Device.OpenUri(new Uri(“app-settings:”));), but there is no such command for Android so you have to use Dependency Service.

    On StackOverflow, EvZ has posted the code how to do this. So here are the files you have to create in your Xamarin Forms app, including all the usings etc.

    .net standard application

    In your .net standard application, add a new interface called “IAppSettingsHelper.cs” with this content (modify Namespace according to your settings):

    using System;
    namespace MyApp.Helpers
    {
        public interface IAppSettingsHelper
        {
             void OpenAppSettings();
        }
    }
    

    Android App

    Create a new class “AppSettingsInterface.cs” in your Android App. Modify “package_name” according to your package, e.g. “com.example.myapp”.

    using System;
    using Android.Content;
    using MyApp.Droid;
    using MyApp.Helpers;
    using Xamarin.Forms;
    using Application = Android.App.Application;
    
    [assembly: Dependency(typeof(AppSettingsInterface))]
    namespace MyApp.Droid
    {
        public class AppSettingsInterface : IAppSettingsHelper
        {
            public void OpenAppSettings()
            {
                var intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
                intent.AddFlags(ActivityFlags.NewTask);
                string package_name = "my.android.package.name";
                var uri = Android.Net.Uri.FromParts("package", package_name, null);
                intent.SetData(uri);
                Application.Context.StartActivity(intent);
            }
    
        }
    }
    

    iOS App

    Finally also create a new class “AppSettingsInterface.cs” in your iOS App. Note that it’s not needed to add your application id here as iOS will automatically open the correct settings.

    using System;
    using MyApp.Helpers;
    using MyApp.iOS;
    using Foundation;
    using UIKit;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(AppSettingsInterface))]
    namespace MyApp.iOS
    {
        public class AppSettingsInterface : IAppSettingsHelper
        {
            public void OpenAppSettings()
            {
                var url = new NSUrl($"app-settings:");
                UIApplication.SharedApplication.OpenUrl(url);
            }
        }
    }
    

    From Xamarin Forms, you could now open app settings on Android or iOS just with this command:

    DependencyService.Get<IAppSettingsHelper>().OpenAppSettings();

     

     

     

     

     

     

Leave a comment

If you want to share your opinion, leave a comment.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

  • Martin says:Reply
    November 1, 2019 at 12:34

    In your Android code you can change:
    string package_name = "my.android.package.name";
    To:
    string package_name = Application.Context.PackageName;
    This will give you the default PackageName so that you won’t have to enter it manually.