Play iOS6 embedded YouTube videos using uiwebview in a single portrait iPhone app

I have an app for iPhone with a storyboard, several xib and custom cells.

The application is installed as a "portrait" as a "supported interface orientation" (I mean, everything looks like this). My user cells have a Uiwebview associated with the embedded video on YouTube, when I click on it, the video will start playing, but my problem is that they always play in portrait mode. I read a lot of things that solve this problem, but only in ios5.

Actually: I can determine when the video starts or stops playing. I can determine the orientation of the device. But I can’t (I want to) switch (Force) the orientation from portrait to landscape or offer this ability if the user changes the orientation of his device.

Thanks in advance.

PS: I can display the code for the identifying material of the application, if necessary.

+8
youtube ios6 uiwebview landscape
source share
2 answers

I had the same problem. My decision:

1. First of all, enable all orientations in the xcode project:

enter image description here

2.In AppDelegate.m add:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { NSArray *stackViewControllers = self.navigationController.viewControllers; UIViewController *rvc = [stackViewControllers objectAtIndex:stackViewControllers.count - 1]; if([rvc isKindOfClass:[VideoViewController class]]) { id presentedViewController = [rvc presentedViewController]; NSString *viewControllerName = NSStringFromClass([presentedViewController class]); if([viewControllerName isEqual:@"MPInlineVideoFullscreenViewController"] && [VideoViewController isVideoPlaying]) { return UIInterfaceOrientationMaskAll; } } return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; } 

In the above code, the system always asks for a supportedInterfaceOrientationsForWindow if you check if the current viewController is where you put the UIWebView with the built-in YouTube player, and also check if the video is playing now (namely, video in full screen).
NOTE. I use UINavigationController , if you do not, you have to make some changes to get the current viewController.

3.In viewController , where I set the UIWebView for the youtube embedded player (in my case, this is VideoViewController ), in its method of adding a header file:

 +(BOOL)isVideoPlaying; 


4.In VideoViewController.m add a static variable:

 static BOOL _isVideoPlaying = NO; 


5.In viewDidLoad add addObserver for notifications to know when the video started , and will use ExitPlaying :

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillExitFullscreen:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil]; 


6. Also add notification selection methods:

 -(void)playerStarted:(NSNotification *)notification{ _isVideoPlaying = YES; } -(void)playerWillExitFullscreen:(NSNotification *)notification { _isVideoPlaying = NO; if([AppUtils iOSVersion] < 6) //For iOS < 6.0, you must manually rotate viewController view when fullscreen video playing is dismissed. { if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) { self.navigationController.view.userInteractionEnabled = NO; [UIView animateWithDuration:0.5 animations:^{ [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO]; // rotate main view, in this sample the view of navigation controller is the root view in main window [self.navigationController.view setTransform: CGAffineTransformMakeRotation(180 * M_PI * 0.5)]; // set size of view [self.navigationController.view setFrame:CGRectMake(0, 0, 320, 960)]; } completion:^(BOOL finished) { self.navigationController.view.userInteractionEnabled = YES; }]; } } } 


7. And add methods to VideoViewController.m :

 +(BOOL)isVideoPlaying { return _isVideoPlaying; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { if(!isVideoPlaying) { return toInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && toInterfaceOrientation != UIInterfaceOrientationLandscapeRight; } return YES; } 


All of these tricks work well for me, supporting iOS 5 and later.
Hope this works for you!

+5
source share

I took Almas Adilbek's answer (very beautifully done!) And welded it to its main component. Only this code alone (added to my application delegate) seems to get the desired results for me. Will be updated if I have problems.

 - (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; } } 
+24
source share

All Articles