I am working on an application for which I would like to:
Requirement 1 is simple: I use AVAudioSessionSoloAmbient as the applicationโs audio session category, so my audio session is disconnected when the ring / silent switch is off.
Requirement 2 seems much more complicated, because I need some kind of callback, notification, or KVO that will allow me to control the position of the switch, but Apple made it clear that it does not want to offer an officially open way of doing this. However, if I find a non-intrusive way to control the position of the switch, even one that is technically prohibited (for example, internal NSNotification ), I would be ready to launch it with Apple.
In addition, I would prefer not to implement some of the polls that I found elsewhere. See an example of the "Related Questions" section.
What I found out (aka that doesn't work)
In versions 4 and 5 of iOS, at least there was a trick that could be used to get the position of the view switch of the route property of the current audio session . Besides the obsolescence of the AVAudioSession class, I can confirm that this trick is no longer an option. The current route, as indicated in C functions containing the legacy Audio Session API and the current AVAudioSession class, does not change when you switch the ring / silent switch.
AVSystemController is an inner class that seems to have promising prospects. Calling - (BOOL)toggleActiveCategoryMuted on sharedAVSystemController really sharedAVSystemController my application sound. In addition, the common singleton sends an AVSystemController_SystemVolumeDidChangeNotification notification when the system volume is changed using the volume buttons. Unfortunately, this notification was not sent in response to changes in the ring / silent switch (although this doubtfully referred source says it should ).
As far as I can tell, there are no NSNotification sent by any object in response to a switch / silence change. I came to this conclusion after adding myself as an observer to all the notifications in the center by default:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:nil object:nil];
and then switch the ringer / tick switch. Nothing.
The AVSystemController class has a promising method with a signature:
- (BOOL)getActiveCategoryMuted:(BOOL*)arg1;
However, there are two problems:
- None of the return values โโor
BOOL pointed to by arg1 will ever change in response to the toggle of the ring / silent switch. - Due to the signature of the method, this method is not (as I understand it) a candidate for KVO.
I suspect that some object is sending some other GSEventRef object (s) when the mute switch is changed, because in the description of event types I see the following:
kGSEventRingerOff = 1012, kGSEventRingerOn = 1013,
However, Iโm sure that I canโt intercept these messages, and even if I could, it would be a little more than โa littleโ intrusive.
Why do I believe it is possible
Simply put: the Instagram app essentially demonstrates this behavior. When you watch a video, it respects the ringer / silence setting, but displays an icon when the switch is off. The icon disappears and appears immediately after moving the switch, which, I believe, should be based on events, not on polling.
Matters Related
This question goes back to iOS 4 and uses the methods that I mentioned in my first brochure above.
This question is very similar to the one above.
This question is (much) more relevant, asking about iOS 7. However, since I am ready to accept a minimally obsessive violation of the private -API, I would argue that this is another question from my own.
This answer suggests using a polling method that I would rather avoid.
ravron
source share