2

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!

SDW
  • 1,880
  • 4
  • 19
  • 30
  • As I explain in my answer, the `- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window` is not the correct place to specify the orientation. For starter, I doubt you actually have more than **1** `UIWindow` in your application. Most apps don't. So you are confusing the iOS by giving different values to `supportedInterfaceOrientationsForWindow` at different times. You should think of that method as an equivalent to the `plist`: static. – SwiftArchitect Jul 25 '14 at 05:16
  • Place chat here: http://chat.stackoverflow.com/rooms/57910/uiviewcontroller-not-closing-from-modal-segue – SDW Jul 25 '14 at 19:47
  • I'm voting to close this question because it's not relevant anymore. Old question with outdated code. Please remove this question. – SDW Apr 03 '17 at 14:11
  • 2
    @Cœur and other close voters: The fact that something is against the TOS of some other site, or even illegal in some jurisdictions, *doesn't* make it inherently off-topic for Stack Overflow. Please explain a valid reason that this question is off-topic. – Makyen Mar 02 '18 at 04:49
  • @Makyen It's a valid reason for me. But it may be worth a meta question. – Cœur Mar 02 '18 at 04:50
  • 1
    @Cœur not valid according to the mods: [see this meta post](https://meta.stackoverflow.com/questions/362135/are-questions-about-downloading-or-converting-youtube-videos-allowed) – River Mar 03 '18 at 02:21

2 Answers2

4

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.

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • You can also get inspired from these two other answers: they are covering all your orientation needs: http://stackoverflow.com/questions/24703427/disable-autorotate-for-view-in-tabview-controller/24708911#24708911 and my response here http://stackoverflow.com/questions/12410031/ios-6-rotations-supportedinterfaceorientations-doesn%C2%B4t-work/24716792#24716792 – SwiftArchitect Jul 25 '14 at 05:21
  • I understand your answer and what I should do, but it just doesn't work. Basically what you say is: Delete all the code. Go to the ViewController where the view should be always portrait and put this code: - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } or??? – SDW Jul 25 '14 at 18:55
  • Basically yes: each view controller is responsible for its orientation. – SwiftArchitect Jul 26 '14 at 04:50
  • Well, when I try to add that code to each view it doens't work. The view wont rotate on the XCDYouTubeVideoPlayerViewController. Let's have a chat so I can explain what I do! – SDW Jul 26 '14 at 13:47
1

In your - (NSUInteger)application:supportedInterfaceOrientationsForWindow, replace "self.window" with "window".

danielmhanover
  • 3,094
  • 4
  • 35
  • 51