Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Changing supported interface orientations programmatically in the View controller .m file problem

I am currently working on a video based application, in the info.plist file I have all orientations supported and in the AppDelegate.m file I have UIInterfaceOrientationPortrait and UIInterfaceOrientationPortraitUpsideDown supported. However the supported Orientations of the AppDelegate only work at the main screen (I am using a Navigation Controller so I think that is what is causing that issue). In one of my ViewControllers (not the main screen) I have a moviePlayer that plays fullscreen when a Table View Item is selected. In the .m file for the view controller the code I have for to play the video is the following (which works by the way):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //Working out of file path for selected video
    NSLog(@"didSelectRowAtIndexPath");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSString *currentFileName = [filePathsArray[indexPath.row] lastPathComponent];
    NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:currentFileName];
    NSURL *videoURL =[NSURL fileURLWithPath:filePath];


    //selectors set
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];


    //Play the movie now
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    self.moviePlayer.view.frame = self.view.frame;
    [self.view addSubview:moviePlayer.view];
    [self.moviePlayer setFullscreen:YES animated:YES];
    [self.moviePlayer play];

    //Deselect Tableview item
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)willEnterFullscreen:(NSNotification*)notification {
    NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
    NSLog(@"enteredFullscreen");
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    appDelegate.fullScreenVideoIsPlaying = @"TRUE";
    NSLog(appDelegate.fullScreenVideoIsPlaying);
}

- (void)willExitFullscreen:(NSNotification*)notification {
    NSLog(@"willExitFullscreen");
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    appDelegate.fullScreenVideoIsPlaying = @"FALSE";
    NSLog(appDelegate.fullScreenVideoIsPlaying);
}

- (void)exitedFullscreen:(NSNotification*)notification {
    NSLog(@"exitedFullscreen");
    [self.moviePlayer.view removeFromSuperview];
    self.moviePlayer = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
    NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    switch ([reason intValue]) {
        case MPMovieFinishReasonPlaybackEnded:
            NSLog(@"playbackFinished. Reason: Playback Ended");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"playbackFinished. Reason: Playback Error");
            break;
        case MPMovieFinishReasonUserExited:
            NSLog(@"playbackFinished. Reason: User Exited");
            break;
        default:
            break;
    }
    [self.moviePlayer setFullscreen:NO animated:YES];
}

The Current Orientation code in the Downloads.m file that does not work:

- (NSUInteger)supportedInterfaceOrientations
{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    if([appDelegate.fullScreenVideoIsPlaying  isEqual: @"TRUE"]){

        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else{

        return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;

    }
}

- (BOOL)shouldAutorotate{
    return YES;


}

appDelegate.fullScreenVideoIsPlaying is declared as NSString in the AppDelegate.h. The reason for this was because I was having issues with it being a Boolean variable. So whenever a video is playing fullscreen it is == "TRUE" and when fullscreen is exited it is == "FALSE"

The if statements I have in the supportedInterfaceOrientations method are never being used and I have tested this by adding NSLog in it and searching for a output, which where never outputted in the debug area. So how do I change the supported interface orientations when the video plays full screen and reset them back to Portrait when fullscreen is exited. Is there another method I should be using? Any help will appreciated this problem is driving me mad.

Just worked it out there doesn't matter anymore