From 76fb5b5d5342d14ef9a1fa7b5f760d3a72710b6d Mon Sep 17 00:00:00 2001 From: Baris Sencan Date: Thu, 10 Dec 2015 21:31:16 -0800 Subject: [PATCH] [MobilePlayer] Fix showContentActions crash on iPads Fixes #107 --- MobilePlayer/MobilePlayerViewController.swift | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/MobilePlayer/MobilePlayerViewController.swift b/MobilePlayer/MobilePlayerViewController.swift index b0f9545..d4e8264 100644 --- a/MobilePlayer/MobilePlayerViewController.swift +++ b/MobilePlayer/MobilePlayerViewController.swift @@ -164,6 +164,7 @@ public class MobilePlayerViewController: MPMoviePlayerViewController { private func initializeControlsView() { (getViewForElementWithIdentifier("playback") as? Slider)?.delegate = self + (getViewForElementWithIdentifier("close") as? Button)?.addCallback( { if let navigationController = self.navigationController { @@ -173,15 +174,22 @@ public class MobilePlayerViewController: MPMoviePlayerViewController { } }, forControlEvents: .TouchUpInside) - (getViewForElementWithIdentifier("action") as? Button)?.addCallback( - showContentActions, - forControlEvents: .TouchUpInside) + + if let actionButton = getViewForElementWithIdentifier("action") as? Button { + actionButton.addCallback( + { + self.showContentActions(actionButton) + }, + forControlEvents: .TouchUpInside) + } + (getViewForElementWithIdentifier("play") as? ToggleButton)?.addCallback( { self.resetHideControlsTimer() self.state == .Playing ? self.pause() : self.play() }, forControlEvents: .TouchUpInside) + initializeControlsViewTapRecognizers() } @@ -336,8 +344,11 @@ public class MobilePlayerViewController: MPMoviePlayerViewController { /// `UIActivityViewController` with `activityItems` set as its activity items. If content is playing, it is paused /// automatically at presentation and will continue after the controller is dismissed. Overriding this method is /// recommended if you want to change this behavior. - public func showContentActions() { - // FIXME: iPad crash on iOS 8+ + /// + /// parameters: + /// - sourceView: On iPads the activity view controller is presented as a popover and a source view needs to + /// provided or a crash will occur. + public func showContentActions(sourceView: UIView? = nil) { guard let activityItems = activityItems else { return } let wasPlaying = (state == .Playing) moviePlayer.pause() @@ -353,6 +364,12 @@ public class MobilePlayerViewController: MPMoviePlayerViewController { self.moviePlayer.play() } } + if let sourceView = sourceView { + activityVC.popoverPresentationController?.sourceView = controlsView + activityVC.popoverPresentationController?.sourceRect = sourceView.convertRect( + sourceView.bounds, + toView: controlsView) + } presentViewController(activityVC, animated: true, completion: nil) }