How to turn on/off airplane mode in Xamarin.Forms for android and ios?
Asked
Active
Viewed 1,092 times
-2
-
You mean you want to set/unset that from your app? Not possible. – Patrick Hofman Jul 09 '19 at 09:48
-
2Imagine you were allowed to do this, what a nasty security hole that would be. – DavidG Jul 09 '19 at 09:49
1 Answers
0
As @Patrick Hofman and @DavidG said.You could not turn on/off airplane mode by code in your project . However , you can open the setting page of system in Forms by using Dependency Service and let user turn on/off airplane mode manual.
in Forms,add a new interface
public interface ISettingsService
{
void OpenSettings();
}
in iOS
using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.iOS
{
public class OpenSettingsImplement : ISettingsService
{
public void OpenSettings()
{
var url = new NSUrl($"App-Prefs:");
UIApplication.SharedApplication.OpenUrl(url);
}
}
}
Note:
App-Prefs:
is private api in iOS. So if you want to upload it to app store , it will may been rejected by the store .
in Android
using Android.Content;
using xxx;
using xxx.Droid;
using Xamarin.Forms;
[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.Droid
{
public class OpenSettingsImplement : ISettingsService
{
public void OpenSettings()
{
Intent intentOpenSettings = new Intent();
intentOpenSettings.SetAction(Android.Provider.Settings.ActionAirplaneModeSettings);
Android.App.Application.Context.StartActivity(intentOpenSettings);
}
}
}

Lucas Zhang
- 18,630
- 3
- 12
- 22