babyghost
babyghost

Reputation: 84

MPInlineVideoFullscreenViewController rotation with UIWebView in iPhone 6/iOS 8

I have an app that is very simple. It reads the XML feed and display the content. Sometimes, the content comes with YouTube video. So, I put the following in the AppDelegate,

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

id presentedViewController = [window.rootViewController presentedViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;

if (window && [className isEqualToString:@"MPInlineVideoFullscreenViewController"]) {
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait;
}
}

After [_window setRootViewController:rootTabBarController]; in the didFinishLaunchingWithOptions. It works perfectly well. The app locks with portrait all the time, and only allows horizontal view if video is playing.

I am now upgrading my app to iOS 8 with iPhone 6/Plus support. I have created a Launch Screen xib to make that work. Everything is fine. The app looks perfectly clear in the iPhone 6/Plus now, no more up-scaling.

However, the above rotation code doesn't work anymore. Any advice? Thanks.

Upvotes: 2

Views: 1045

Answers (2)

ehrpaulhardt
ehrpaulhardt

Reputation: 1657

Under iOS8 you have to check for AVFullScreenViewController instead of MPInlineVideoFullscreenViewController

example:

static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS7 = @"MPInlineVideoFullscreenViewController";
static NSString * const VIDEO_CONTROLLER_CLASS_NAME_IOS8 = @"AVFullScreenViewController";

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if ([[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS7)] ||
    [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(VIDEO_CONTROLLER_CLASS_NAME_IOS8)]) {

        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

Upvotes: 4

Say2Manuj
Say2Manuj

Reputation: 709

A more cleaner way to handle orientation support for MPMoviePlayerController is to add observers for MPMoviePlayer notifications (MPMoviePlayerWillEnterFullscreenNotification and MPMoviePlayerWillExitFullscreenNotification) in your AppDelegate. Whenever a notification is received set allowOrientation flag to YES when entering Full Screen and to NO when exiting Full screen. Since these notifications are received before a call to supportedInterfaceOrientationsForWindow is made by system so the allowOrientation flags can be used in supportedInterfaceOrientationsForWindow function to set appropriate orientation.

IMPLEMENTATION: 

@interface AppDelegate ()

@property(assign) BOOL allowOrientationChange;

@end


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.allowOrientationChange = NO;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(setAllowOrientationChangeFlagToYES:)
                                                 name:MPMoviePlayerWillEnterFullscreenNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(setAllowOrientationChangeFlagToNO:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:nil];

    return YES;
}

-(void)setAllowOrientationChangeFlagToYES:(NSNotification*)aNotification{
    self.allowOrientationChange = YES;
}

-(void)setAllowOrientationChangeFlagToNO:(NSNotification*)aNotification{
    self.allowOrientationChange = NO;
}



- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    if (self.allowOrientationChange) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

Upvotes: 0

Related Questions