Skip to content

Instantly share code, notes, and snippets.

@MylesCaley
Last active February 1, 2019 03:56
Show Gist options
  • Save MylesCaley/75a08a3006ad0109271b5fdc5075dfc7 to your computer and use it in GitHub Desktop.
Save MylesCaley/75a08a3006ad0109271b5fdc5075dfc7 to your computer and use it in GitHub Desktop.
When you have a portrait-only app you may still want to allow rotation when the app plays full screen media. This is a decent solution by creating a notification to signal a "full screen". Only tested on iOS9.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var isFullScreen = false
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// create notifications from wherever to signal fullscreen
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.willEnterFullScreen(_:)), name: "MediaEnterFullScreen", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.willExitFullScreen(_:)), name: "MediaExitFullScreen", object: nil)
return true
}
func willEnterFullScreen (notification: NSNotification) {
isFullScreen = true
}
func willExitFullScreen (notification: NSNotification) {
isFullScreen = false
}
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return isFullScreen == true ? UIInterfaceOrientationMask.All : UIInterfaceOrientationMask.Portrait
}
}
@ingconti
Copy link

ingconti commented May 24, 2018

for swift 4:

....

var isFullScreen = false

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

	// create notifications from wherever to signal fullscreen
	NotificationCenter.default.addObserver(self,
												 selector: #selector(willEnterFullScreen(notification:)),
												 name: NSNotification.Name(rawValue: "MediaEnterFullScreen"),
												 object: nil)
	NotificationCenter.default.addObserver(self,
												 selector: #selector(willExitFullScreen(notification:)),
												 name: NSNotification.Name(rawValue: "MediaExitFullScreen"),
												 object: nil)

	return true
}




@objc func willEnterFullScreen (notification: NSNotification) {
	isFullScreen = true
}

	@objc func willExitFullScreen (notification: NSNotification) {
	isFullScreen = false
}

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
	return isFullScreen == true ? UIInterfaceOrientationMask.all : UIInterfaceOrientationMask.portrait
}

---- if You need to play, in your controller:

import UIKit
import AVKit

class FirstViewController: UIViewController {

override func viewDidLoad() {
	super.viewDidLoad()
	playVideoiOS9()
	}

func playVideoiOS9() {

	
	if let url = URL(string: "http://devstreaming.apple.com/videos/wwdc/2016/102w0bsn0ge83qfv7za/102/hls_vod_mvp.m3u8"){
		
		let player = AVPlayer(url: url)
		let controller=AVPlayerViewController()
		controller.player=player
		controller.view.frame = self.view.frame
		self.view.addSubview(controller.view)
		self.addChildViewController(controller)
		player.play()
	}
}

}

@ingconti
Copy link

note anyway those calls are deprecated since ios9 and as per Apple ADC docs, notifications are 👍

MPMoviePlayerWillEnterFullscreen
and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment