Video icon 64
Learning to code? Skill up faster with our practical video courses. Start your free trial today.
Advertisement

iOS SDK Quick Tip: Enabling AirPlay for iOS 4.3 Apps

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

One of the most impressive features released with iOS 4.3 is official SDK support for AirPlay integration. With just a few lines of code, any iOS application can now stream video directly to an AppleTV! AirPlay will also work with other supported Apple devices, like the AirPort Express. Read on to learn how to integrate AirPlay into your apps!

In a previous tutorial, I demonstrated how to play video on iOS4 with the MediaPlayer Framework. This tutorial will extend the Big Buck Bunny iOS app created in that tutorial by incorporating the new AirPlay functionality offered in SDK 4.3.


Step 1: Enable Airplay

Enabling AirPlay on any MPMoviePlayerController object is simple:

[moviePlayerController setAllowsAirPlay:YES];

Setting the allowsAirPlay property to YES will cause an additional icon to appear alongside the movie player controls that will initiate AirPlay playback. Unfortunately, developers aren't given direct control over this feature, and must depend on the default functioning of this control.

After enabling AirPlay, the Big Buck Bunny video player controls should look like this:

AirPlay Button

After you begin to stream to an AirPlay device, the Big Buck Bunny screen will automatically display a message notifying the user:

Airplay Playing Message

Of course, you could have also set the allowsAirPlay value via dot notation:

moviePlayerController.allowsAirPlay = YES;

Step 2: Make AirPlay Backwards Compatible

Running the line of code from Step 1 on older versions of iOS that don't support the allowsAirPlay property will result in a run-time exception that will crash your app. To offer AirPlay only to those devices that support it, we need to place a conditional around the statement that will check to see if the MPMoviePlayerController object supports the allowsAirPlay option.

To do so, update the code to use the respondsToSelector: method:

if([moviePlayerController respondsToSelector:@selector(setAllowsAirPlay:)])
{
    [moviePlayerController setAllowsAirPlay:YES];
}

The snippet above will enable AirPlay when it is supported and simply skip the code block on unsupported devices.


Additional Considerations

There are a few things to keep in mind when implementing AirPlay in your projects:

1. AirPlay is Not Supported on Older Devices.

iPhone 3G users despair: no AirPlay for you. Apple has a habit of making new SDK and iOS features only available on the most recent devices, and this is true with the AirPlay addition to SDK 4.3.

The support issues aren't limited only to older iPhone devices, either. Older versions of the AppleTV are also rumored to not support the new AirPlay features.

2. Users Must Update AppleTV and iOS Software

When launching an AirPlay enabled app, keep in mind that your users will need to have both a recent iOS version on their device AND an updated version of the AppleTV software. Your application may be working perfectly, but an older OS version running on an Apple TV can still steal your user's joy.

3. Users Must Share Network with AirPlay Devices

In order for iOS to find other Apple AirPlay enabled devices, you will need to join the same WiFi network that the AirPlay devices are connected to. An Edge/3G connection without a wireless signal is not enough!

Advertisement