I'm making a news app and one of the big features for this app is having a view where a video can be played. So I was looking on cocoacontrols.com and found this control: https://github.com/0xced/XCDYouTubeKit
I got this plugin working and well, but now the problem starts:
For this app the orientation needs to be portrait at all the time. No landscape or upside down. The thing here is that you also need to watch a video in portrait. Something that no one wants right? Everybody wants to watch video's in landscape!
Now I found some code that helps me a lot and set the XCDYouTubeVideoPlayerViewController
available for portrait and landscape:
In my appDelegate I add this code:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController]
isKindOfClass:[XCDYouTubeVideoPlayerViewController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
if ([[self.window.rootViewController presentedViewController]
isKindOfClass:[UINavigationController class]]) {
// look for it inside UINavigationController
UINavigationController *nc = (UINavigationController *)[self.window.rootViewController presentedViewController];
// is at the top?
if ([nc.topViewController isKindOfClass:[XCDYouTubeVideoPlayerViewController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
// or it's presented from the top?
} else if ([[nc.topViewController presentedViewController]
isKindOfClass:[XCDYouTubeVideoPlayerViewController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
}
}
This code works just fine. The video can be played in portrait as well in landscape. Now when the video ends in landscape or the user ends the video in landscape and go back to the view that they came from the app will be displayed in landscape. I have to turn my device in portrait so it will be stay that way.
The question is: How can I edit the above code so the landscape will be disabled when the video ends in landscape or the user ends the video in landscape. It just needs to be back in portrait when the video ends and go back!
Delete that code!
From the documentation:
application:supportedInterfaceOrientationsForWindow:
This method returns the total set of interface orientations supported by the app. When determining whether to rotate a particular view controller, the orientations returned by this method are intersected with the orientations supported by the root view controller or topmost presented view controller. The app and view controller must agree before the rotation is allowed.
If you do not implement this method, the app uses the values in the UIInterfaceOrientation key of the app’s Info.plist as the default interface orientations.
You should use this instead:
- (NSUInteger)supportedInterfaceOrientations
in your UIViewController
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller’s shouldAutorotate method returns YES.