Toggling Airplane Mode in iOS Programmatically

How can i know airplane mode on or off in ios

AFAIK, there is no built-in api to determine if Airplane mode is on. You have to check if the respective services are available, depending on what you need in your app.

For example, if you need a GPS location, you could check if a GPS location is available and then handle your different cases. Same for a network connection and all the other services that are disabled when airplane mode is on.

Rechability is one example for checking network connection.

How to turn on/off airplane mode in IOS 5.1 using private API

As jrtc27 describes in his answer (and I mentioned here), you need to grant your app a special entitlement in order to successfully change the airplaneMode property.

Here's a sample entitlements.xml file to add to your project:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.SystemConfiguration.SCDynamicStore-write-access</key>
<true/>
<key>com.apple.SystemConfiguration.SCPreferences-write-access</key>
<array>
<string>com.apple.radios.plist</string>
</array>
</dict>
</plist>

com.apple.radios.plist is the file where the airplane mode preference is actually stored, so that's what you need write access to.

No, you don't need to use dlopen or dlsym to access this API. You can add the AppSupport framework to your project directly (with the exception that AppSupport.framework is stored on your Mac under the PrivateFrameworks folder). Then, just instantiate a RadiosPreferences object, and use it normally. The entitlement is the important part.

For your code, first use class-dump, or class-dump-z, to generate the RadiosPreferences.h file, and add it to your project. Then:

#import "RadiosPreferences.h"

and do

RadiosPreferences* preferences = [[RadiosPreferences alloc] init];
preferences.airplaneMode = YES; // or NO
[preferences synchronize];
[preferences release]; // obviously, if you're not using ARC

I've only tested this for a jailbroken app. I'm not sure if it's possible to acquire this entitlement if the device isn't jailbroken (see Victor Ronin's comment). But, if this is a jailbreak app, make sure you remember to sign your executable with the entitlements file. I normally sign jailbreak apps with ldid, so if my entitlements file is entitlements.xml, then after building in Xcode without code signing, I would execute

ldid -Sentitlements.xml $BUILD_DIR/MyAppName.app/MyAppName

Here's Saurik's page on code signing, and entitlements



Related Topics



Leave a reply



Submit