Advertisement
iOS SDK

iOS SDK Quick Tip: Enabling AirPlay for iOS 4.3 Apps

by

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!

Related Posts
  • Computer Skills
     
    An Introduction to Bluetooth, Mac and iPhoneBluetoothpreview
    Bluetooth is a wireless protocol that allows the wireless connection of devices. In this tutorial I’ll explain what Bluetooth is, I'll show you how to use an iPhone to unlock a Mac, how to enter text on an iPhone with the Mac's keyboard and how to use a Mac to make and receive phone calls. All with the help of Bluetooth.Read More…
  • Computer Skills
    Hardware
    Getting Started With Google Chromecast and a MacChromecast
    For those invested in the Apple ecosystem, an Apple TV is the way to send audio and video from a Mac or iOS device to a television. Now, there's another way.Read More…
  • Code
    News
    Android 2013: A Year in Review and 2014 PredictionsPk45fe preview image@2x
    It was another strong and, may I say, dominant year for the Android platform. It continued to dominate in market share and climb in numbers. Countless devices were released featuring the popular, mobile operating system and not all of those devices were smartphones or tablets. Android is making headway in new territories. 2013 was a busy year for the green robot. Now that the year has come to a close, it's time to recap the action.Read More…
  • Code
    News
    iOS 2013: A Year in Review and 2014 PredictionsPreview image@2x
    For iOS developers, 2013 was a pivotal year. I'd even go as far as saying that it was the most significant year since the introduction of the iPhone, almost seven years ago. The reason is of course the unveiling of iOS 7. Let's take a look at some of the key moments of 2013 and take a sneak peak at what 2014 has in store for us.Read More…
  • Code
    iOS SDK
    iOS 7 SDK: Core Bluetooth - Theoretical OverviewIcon ios7 v3
    The Core Bluetooth (CB) framework provides the necessary resources for iOS apps to communicate with devices that are equipped with Bluetooth Low Energy technology (BTLE). This tutorial will guide you through the evolution of CB from iOS 5 to iOS 7. Additionally, you will learn how to configure a Core Bluetooth central and peripheral, how to communicate between them, and some best programming practices.Read More…
  • Computer Skills
    Media
    Mac mini Media CentreMediacentre400
    According to analysts – Gene Munster, of Piper Jaffray, in particular – Apple is soon to launch a revolutionary Apple TV device. Don’t mistake this for another Apple “hobby”; it’s not going to be a set-top box. This is going to be the real-deal, Siri-enabled, iCloud-attached, iOS-controlled, large flat-panel to dominate your living room! Apparently. But since Gene has been banging on about this for three years or more (Apple has it’s “hobby”, and Gene has his), the Mac mini makes for a pretty good media centre in the meantime. In this tutorial, I’ll examine some of the possibilities and options for making a Mac mini into a media centre.Read More…