-->

Toggling AirPlane mode in iOS Programmatically

2019-01-11 11:29发布

问题:

I want to on/off airplane mode in iOS programmatically. After googling, i got some of the following links, and followed them,

Reachability airplane mode (3G) vs. Wifi

Using Private Framework: Importing RadioPreferences.h

Detect if iPhone is in Airplane mode?

All of the above links, tell me to use AppSupport.framework and using RadiosPreferences.h. This is giving me the status about whether airPlane mode is on or off, But it doesn't make me to change the airplane mode.

Then i saw, this link, How to turn on/off airplane mode in IOS 5.1 using private API, that gives me the way for toggling airplane mode but it is using entitlements, which only works for jailbroken devices. Is there any way, to set Airplane mode programatically(can use private APIs but not entitlements).

回答1:

Every single App Store app is sandboxed with entitlements.

What you are trying to achieve is simply not possible if you want to distribute your app in the App Store. Your App simply cannot get enough privileges to achieve anything remotely similar. You cannot set global settings that changes the behaviour of the system. Apple will never allow that.

At the best you can monitor the status of the network connectivity via reachability but this is pretty much it. Using private API = straight app rejection.



回答2:

If you don't care about AppStore submission and if your device is jailbroken (up to iOS 7.0.4 are jailbreakable), then you can follow these steps in exact order to programatically turn on or off Airplane Mode. I am also attaching a sample project to make things easier.

  1. Download (https://iosopendev.googlecode.com/files/iOSOpenDev-1.6-2.pkg) and install. This will install the necessary tools in order to directly install the app in the place where Apple's apps are located. This is needed so that you have a proper access and permission.

  2. Install OpenSSH in your device using Cydia. Then follow this tutorial: (http://www.priyaontech.com/2012/01/ssh-into-your-jailbroken-idevice-without-a-password/) to enable access without password (Using SSH key).

  3. Download the sample project I created from here: (https://www.dropbox.com/s/c0i76vmtfckvsut/ToggleAirplane.zip) This project uses the same entitlements as SpringBoard app (App that controls home screen) of Apple. So you will have access to every setting. You will only need to know how to call it programatically.

  4. In build settings go to "User Defined" section and edit the value for key iOSOpenDevDevice to the IP address of your device.

  5. Now most awesome part. Press Command + Shift + I. This will install and respring your device.

  6. However you may not see the app. To see it, download the Respring app from Cydia. When you launch it, it will respring the device. You should be able to see the app "ToggleAirplane" on your home screen.

Note: You won't be able to delete it from home screen just like you cannot do with Apple's apps. To delete it, you will need to go to Cydia->Packages. Fine the app and uninstall.