Play Audio While the Silent Switch is ON

I’d receive emails from time to time complaining my apps that have no sound. However, its actually caused by the user turing on the Silent Switch…

To enable audio playing even when the user turned on the silent switch. Simply put the following code in your app delegate while its launching.

[code lang=objc]
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[audioSession setActive:YES error:nil];

[/code]

The AVAudioSessionCategoryPlayback category enable your app to play audio while silent switch is on (and even the screen is locked). AVAudioSessionCategoryOptionMixWithOthers allow your audio to mix with other audio from other apps, and of course, its optional.

Mute Checking in iOS7

Up to iOS 7 there is no simple API to check whether the user switched on the mute button(silent switch) or not…

Sharkfood produced a class to constantly check if a user switched on the mute button.

The idea is playing a short(0.2 sec) muted sound file using AudioServices. If it takes longer then 0.2 sec to complete, mute button is off. Otherwise if it finished very soon, say shorter then 0.1 sec, mute button is on.

However, in most case I don’t need to keep checking mute in the run loop. I just want to know if it is muted while the user start playing some audio. So I modified it and made a class, named MuteChecker, to check it on-demand.

Also, it will call a completion block after checking. So you can put whatever logic you like for the checking result.

To use it, you just simply initialise it with a completion block.

[code lang=objc]
self.muteChecker = [[MuteChecker alloc] initWithCompletionBlk:^(NSTimeInterval lapse, BOOL muted) {
}];
[/code]

The “lapse” parameter return the time used for playing the checking sound.

The “muted” parameter will return YES if the time lapse is < 0.1 sec.

To start checking, call

[code lang=objc]
[_muteChecker check];
[/code]

and your result will be reflected by the completion block.

You can download a sample project here.

Changing UISearchBar Button Color

To change the UISearchBar button color, the simplest approach is to change the Tint color in it’s View section using storyboard.

However, it will also affect the insertion point color.

So if you want a white color button, the insertion point will disappear because is the same color as the search bar background…

To tackle this problem, we can configure the appearance of the search bar using its appearance proxy in the app delegate:

[code lang=objc]
NSDictionary *attrDict = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
UIBarButtonItem *buttonItemProxy = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
[buttonItemProxy setTitleTextAttributes:attrDict forState:UIControlStateNormal];
[/code]

This will only change the color of the button but not the insertion point.

Note that this approach will affect all search bars in your app.

Tab Bar Background Color and Button Tint Color

To change the background color of tab bar, we can change the tab bar setting in storyboard.

However, unlike navigation bar, we cannot setting the tab bar item’s tint color inside storyboard.

We have to add some code in the app delegate, inside application:didFinishLaunchingWithOptions:, add

[code lang=objc]
[[UITabBar appearance] setTintColor:[UIColor yellowColor]];
[/code]

Note that this is an application-wide setting, all tab bar items will tint to the specific color.

Navigation Bar Background Color and Buttons Tint Color

To change the background color of navigation bar and the tint color of bar button items, no code is needed.

In the storyboard, click on the navigation bar of your navigation controller.

Set the Bar Tint for changing the background color. And Tint inside the View section.

Below shows how to set the background to blue and buttons to white.