Search code examples
xcodevideoios7youtubelandscape

Landscape mode only when playing video. xcode. ios 7


I use youtube-ios-player-helper: https://github.com/youtube/youtube-ios-player-helper and https://developers.google.com/youtube/v3/guides/ios_youtube_helper

I have everything working well. Video playback is fine! BUT! In the project settings, I turned off landscape mode.Therefore, the video only plays in portrait mode. How do I turn on landscape mode when playing video in iOS 7?


Solution

  • My solution:

    it works for iOS 7 and iOS 8

    To play a video from YouTube, I used XCDYouTubeKit (https://github.com/0xced/XCDYouTubeKit)

    AppDelegate.h:

    @property (nonatomic) BOOL screenIsPortraitOnly;
    

    AppDelegate.m:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    
        if (!self.screenIsPortraitOnly) {
            return UIInterfaceOrientationMaskPortrait;
        }
        else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    

    MYTableViewController.m:

    #import "XCDYouTubeVideoPlayerViewController.h"
    #import "XCDYouTubeKit.h"
    #import "AppDelegate.h"
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:myIdYoutube];
        [[NSNotificationCenter defaultCenter] removeObserver:videoPlayerViewController  name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerViewController.moviePlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayerViewController.moviePlayer];
        videoPlayerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:videoPlayerViewController animated:YES completion:nil];
    }
    
    -(void)videoFinished
    {
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        appDelegate.screenIsPortraitOnly = false;
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
    

    XCDYouTubeVideoPlayerViewController.m

    #import "AppDelegate.h"
    
    - (instancetype) initWithVideoIdentifier:(NSString *)videoIdentifier
    {
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        appDelegate.screenIsPortraitOnly = true;
        if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8)
            self = [super initWithContentURL:nil];
        else
            self = [super init];
    
        if (!self)
            return nil;
    
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    
        if (videoIdentifier)
            self.videoIdentifier = videoIdentifier;
    
        return self;
    }
    
    - (void) viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        appDelegate.screenIsPortraitOnly = false;
    
        if (![self isBeingDismissed])
            return;
    
        [self.videoOperation cancel];
    }