Abhishek Drolia
Abhishek Drolia

Reputation: 1

How to open Device Settings in a .Net MAUI App

Can anyone please tell how to open Device Settings in a .Net MAUI App? The only answer I could get suggests using AppInfo.Current.ShowSettingsUI(); but that opens the settings menu or page for the application only, whereas I need to open the general Settings App (or the device Settings App) on all platforms.

Also, how to open a particular section of settings like, for example, Network Settings or Display Settings of the device?

Thanks.

Upvotes: 0

Views: 799

Answers (1)

Leon Lu
Leon Lu

Reputation: 9244

For the Android platform, you could refer to the following steps.

Step 1: Create a static MainActivity variable named Instance, and implement this function:

public class MainActivity : MauiAppCompatActivity  
{  
    public static MainActivity Instance { get; private set; }  
    protected override void OnCreate(Bundle savedInstanceState)  
    {  
        base.OnCreate(savedInstanceState);  
        Instance = this;       
    }  
}

Step 2: Create a static helper class in your Platforms/Android folder, and implement the static method:

using Intent = Android.Content.Intent;
using Setting = Android.Provider.Settings;
public static class MyHelper  
    {  
        public static void start()  
        {  
            MainActivity.Instance.StartActivity(new Intent(Setting.ActionSettings));  
        }  
    }  

Step 3: Call this Android method on MAUI button click event:

#if ANDROID 
private void OnButtonClicked(object sender, EventArgs e)  
       {  
           YourProject.Platforms.Android.MyHelper.start();   
       }
#endif 

For the iOS platform, the OpenAsync method of Launcher can achieve your demand:

#if IOS 
private async void OnButtonClicked(object sender, EventArgs e)  
{  
      try
      {
          await Launcher.OpenAsync("App-Prefs:root");
      }
      catch{}
}
#endif 

If you want to open the specific part of the setting, you can add the function name after the string, such as prefs:root=Brightness and prefs:root=General&path=Network. For more related instances, you can refer to the answer of iOS Launching Settings -> Restrictions URL Scheme.

Upvotes: 0

Related Questions