IOS - How to display the AirPlay popup menu in Swift?
In the end, it seems like there is no easy and simple way to make a custom Airplay system menu display button.
However, @totiG will point me to an interesting resource, and I created a script that creates a standard volume control outside the visible area of ββthe screen, simulates a click on the Airplay button:
func showAirplay() {
let rect = CGRect(x: -100, y: 0, width: 0, height: 0)
let airplayVolume = MPVolumeView(frame: rect)
airplayVolume.showsVolumeSlider = false
self.view.addSubview(airplayVolume)
for view: UIView in airplayVolume.subviews {
if let button = view as? UIButton {
button.sendActions(for: .touchUpInside)
break
}
}
airplayVolume.removeFromSuperview()
}
After running this code, the following popup menu appears:
source to share
There is very little workaround here for using the MPVolumeView button.
- Create an MPVolumeView and hide it somewhere in the view hierarchy.
- Whenever you want to display the collector:
[[UIApplication sharedApplication] sendAction:NSSelectorFromString(@"_displayAudioRoutePicker") to:myVolumeView from:myView forEvent:nil];
Optional 3: On iPad, you will need to pass the UIEvent, otherwise the popover will just be entered at the top of the screen and it will look awkward. Grab the event from - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
and pass it to our call.
source to share
I'm not sure if you need help with the ether aspect, or if you just need a popup menu with this design (or both). I'm not familiar with how programming broadcast works, but to create a popup of this style, you can use a UIAlertController:
let ac = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "iPhone", style: .default, handler: { (action) in
//do airplay stuff that connects to the iPhone
}))
ac.addAction(UIAlertAction(title: "MacBook Pro", style: .default, handler: { (action) in
//do airplay stuff that connects to the MacBook Pro
}))
ac.addAction(UIAlertAction(title: "Apple TV 3", style: .default, handler: { (action) in
//do airplay stuff that connects to the Apple TV 3
}))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(ac, animated: true, completion: nil)
If you need with AirPlay stuff, here's a link to Apple's documentation for streaming, with some helpful videos and documentation: https://developer.apple.com/airplay/
source to share