AirPlay 3.0.1

AirPlay 3.0.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by eMdOS.



AirPlay 3.0.1

AirPlay

AirPlay lets users track iOS AirPlay availability and provides extra information about AirPlay connections.

Development Environment

  • Xcode 8.2.1 (8C1002)
  • iOS 8+
  • CocoaPods 1.2.0

Support

Currently, this library is a kind of workaround to be able to track AirPlay availability observing changes on MPVolumeView. So, it needs to be tested every new iOS release.

If there is an Apple TV or other AirPlay-enabled device in range, the route button allows the user to choose it. If there is only one audio output route available, the route button is not displayed.

PLEASE READ:

When developers provide workarounds (like I’m doing “to detect” AirPlay’s availability) related to private APIs or closed classes (via KVO), Apple uses to stop supporting those properties in order to push developers to stop using them.

Seems like for iOS 10+, Apple stopped supporting alpha property from MPVolumeView. So, it could results in always having isAvailable static property returning true; I’m unsure, I need to dig deeper and do a more exaustive QA.

Please let me know if you find any issue.

Usage

Notifications, Properties, Methods, Closures

Notifications

Notification Description
.airplayAvailabilityChangedNotification Notification sent everytime AirPlay availability changes.
.airplayRouteStatusChangedNotification Notification sent everytime AirPlay connection route changes.

Properties

Property Description
isAvailable RReturns true or false if there are or not available devices for casting via AirPlay. (read-only)
isBeingMonitored Returns true or false if AirPlay availability is being monitored or not. (read-only)
isConnected Returns true or false if device is connected or not to a second device via AirPlay. (read-only)
connectedDevice Returns Device’s name if connected, if not, it returns nil. (read-only)

Methods

Method Description
startMonitoring() Starts monitoring AirPlay availability changes.
stopMonitoring() Stops monitoring AirPlay availability changes.

Closures

Closure Description
whenAvailable Closure called when is available to cast media via AirPlay.
whenUnavailable Closure called when is not available to cast media via AirPlay.
whenRouteChanged Closure called when route changed.

Start Monitoring

What I use to do is to start monitoring in the AppDelegate. It can be implemented anywhere.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    AirPlay.startMonitoring()
    return true
}

Adding/Removing Observers

To add them:

NotificationCenter.default.addObserver(
    self,
    selector: #selector(<selector>),
    name: .airplayAvailabilityChangedNotification,
    object: nil
)
NotificationCenter.default.addObserver(
    self,
    selector: #selector(<selector>),
    name: .airplayRouteStatusChangedNotification,
    object: nil
)

To remove them:

NotificationCenter.default.removeObserver(
    self,
    name: .airplayAvailabilityChangedNotification,
    object: nil
)
NotificationCenter.default.removeObserver(
    self,
    name: .airplayRouteStatusChangedNotification,
    object: nil
)

Using closures

When available:

AirPlay.whenAvailable = { [weak self] in
    <code>
}

When unavailable:

AirPlay.whenUnavailable = { [weak self] in
    <code>
}

When route changed:

AirPlay.whenRouteChanged = { [weak self] in
    <code>
}

Displaying AirPlay availability status

AirPlay.isAvailable will return true or false.

Displaying AirPlay connection status

AirPlay.isConnected will return true of false.

Displaying connected device name

AirPlay.connectedDevice ?? "Unknown Device"

Migration from v1.+ to v3.+

I skipped version 2 just to match with the swift language version.

Changed

  • Notifications naming.

    • .airplayAvailabilityChangedNotification

      … was AirPlayAvailabilityChangedNotification.

    • .airplayRouteStatusChangedNotification

      … was AirPlayRouteStatusChangedNotification.

  • Properties naming.

    • AirPlay.isAvailable

      … was AirPlay.isPossible.

  • Closures naming.

    • AirPlay.whenAvailable

      … was AirPlay.whenPossible.

    • AirPlay.whenUnavailable

      … was AirPlay. whenNotPossible.

    • AirPlay.whenRouteChanged

      … was AirPlay.whenConnectionChanged.

Author

eMdOS

License

AirPlay is available under the MIT license. See the LICENSE file for more info.