Russ McBride
Russ McBride

Reputation: 51

Using volume buttons to toggle airplane mode on iPhone

This is a hard one. I toggle airplane mode a dozen times a day and want a fast way to do it. I'm the rare user who would kill for a physical button dedicated to airplane mode. I'd like to try and do it with the down-volume button. This won't be on the app store and I'm happy (and indeed it will be necessary I think) to use some private libraries. In fact, it may turn out that I need to jailbreak the phone. I'm looking for general architecture advice at this stage, not specific code (although that never hurts).

So the question is: what's the best architecture for approaching this problem? I break down the problem this way:

1) How to keep the app running in the background so it can listen for and respond to volume button presses (and perhaps down-volume events from remotecontrol devices). I've got three choices here: --create a VOIP app --create a navigation app --create a audio app An audio app seems the most reasonable way. Am I going to have to continuously play a silent track on repeat to keep my app alive in the background? It seems like I'll have problems with the app going dead if I play an audio track from another app.

2) How to intercept to volume-down events. One answer to this issue: program access to iPhone volume buttons

3) How to check the airplane status and change it. An old answer to this issue with some Mach code and dynamic linking I don't understand: http://blogs.oreilly.com/iphone/2009/01/bring-airplane-mode-control-ba.html

The best resource for understanding the private libraries seems to be here: http://theiphonewiki.com/wiki/index.php?title=/System/Library/Frameworks

Any suggestions for better private library "docs" or an update to the "iPhone Open Application Development" book by Zdziarski would be welcome.

Thanks for thinking about this with me!

Upvotes: 3

Views: 1730

Answers (1)

Victor Ronin
Victor Ronin

Reputation: 23268

I think, it's better to break it down to multiple separate questions, because all of them are unrelated.

1) On backgrounding. I would recommend to use LocationManager and turn it on and off periodically. This reset the timer which is counting how much time you can stay in the background.

Here are a few useful links:

iPhone - Backgrounding to poll for events

setKeepAliveTimeout and BackgroundTasks

http://www.slideshare.net/tranq72/ios4-multitasking-development-notes

2) No idea.

3) That method won't work anymore, because SBSetAirplaneModeEnabled api is removed.

However, I can explain how the code in provided link works:

#define SBSERVPATH  "/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices"

These are just defines which store location of different dynamically loaded libraries.

void *sbserv = dlopen(SBSERVPATH, RTLD_LAZY);

This will load dynamically loaded library

int (*setAPMode)(mach_port_t* port, BOOL yorn) = 
         dlsym(sbserv, "SBSetAirplaneModeEnabled");

This code does two things. dlsym finds and address of SBSetAirplaneModeEnabled function in this dynamically loaded library. And stores it in setAPMode variable.

setAPMode(p, yorn);

This will just call this function.

dlclose(sbserv);

This will close handle (we don't need it anymore).

You will see a lot mach_port_t as part of many private API calls. Generally this mach port is part of inter process communication system

Here is very brief explanation. In order to communicate with other application, you need to send a message to it and mach_port_t is the handle through which you send these messages. As example, setAPMode internally send a message to SpringBoard application, which turned on/off airplane mode. That's the reason why we have to pass it to these functions.

There are different ways to get a port of other applications. However, most common one for private API is to call another private API which returns the port to you.

There is an API, SBSSpringBoardServerPort, which returns Springboard server port and which you pass to other Sprinboard client side APIs.

Upvotes: 2

Related Questions