iOS 9~ Experiments - New API Components - Multitasking on iPad with Picture-in-Picture (PiP).
- 
= XCode 8.0. 
- 
= Swift 3. 
- 
= iOS 9.0. 
- 
= iPad Mini 3 / 4, iPad Air / Air 2, iPad Pro 
Tested on iOS 9.1 Simulators. 50%
this is the Xcode 8 / Swift updated project.
WWDC 2015 Sessions from : WWDC 2015 Videos Jamie XX - Loud Places (feat. Romy) from : youtube.com
To support HTTP hosted : add the Boolean type Value to YES for NSAllowsArbitraryLoads in app's info.plist file.
- To run the example project, download or clone the repo.
- MULTITASKING RECOMMANDATIONS : Perform and prepare your app to avoid memory leaks and others with Instruments!
- IN USE OF AVPictureInPictureController, use :AVPictureInPictureController.isPictureInPictureSupported()to get devices capability
- Set your AVPlayer,AVPlayerViewControllerorAVPlayerLayerin one controller to play video!
- Add AVKit+AVFoundationFrameworks
import AVKit
import AVFoundation- Set AVAudioSessionindidFinishLaunchingWithOptionsUIApplication Delegate
/// REQUIRED FOR PIP!!!
/// After active Audio/Airplay Background Mode in General Settings.
/// Setup Audio Session for Picture in Picture
let audioSession = AVAudioSession.sharedInstance()
do {
    try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
catch {
    print("handle errors setting Audio session Category")
}- Delegate your controller with AVPlayerViewControllerDelegateExtension
extension ViewController : AVPlayerViewControllerDelegate {
    /// playerViewController will start PIP
    func playerViewControllerWillStartPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP will start")
    }
    /// playerViewController did start PIP
    func playerViewControllerDidStartPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP did start")
    }
    /// playerViewController will stop PIP
    func playerViewControllerWillStopPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP will stop")
    }
    /// playerViewController did stop PIP
    func playerViewControllerDidStopPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP did stop")
    }
    /// playerViewController failed to start PIP
    func playerViewController(playerViewController: AVPlayerViewController, failedToStartPictureInPictureWithError error: NSError) {
        print("PIP Error : \(error.localizedDescription)")
    }
    /// playerViewController automatically dismiss at PIP Start.
    func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(playerViewController: AVPlayerViewController) -> Bool {
        return false
    }
    /// playerViewController restore Interface For PIP
    func playerViewController(playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: (Bool) -> Void) {
    // if topView and navigation controller :
        if let topViewController = navigationController?.topViewController {
            topViewController.presentViewController(playerViewController, animated: true) {
                print("ready detailvc restore presentViewController")
                completionHandler(true)
            }
        }
        else {
            completionHandler(false)
        }
    }
}Build and Run! Switch to Home or another App in your simulator or devices and test!




