Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube Player Orientation Issue on iOS 6

Tags:

ios

iphone

I am trying to play a YouTube video inside a UIWebView (see code below).

Everything is working fine, but the YouTube player doesn't support orientation changes on iOS 6. My entire app is in portrait mode only.

How can I fix this issue? Any help is appreciated.

The code I am using is below:

- (void)viewDidLoad

{        
   [super viewDidLoad];
   float width = 300.0f;
   float height = 300.0f;
   youTubeURL = @"http://www.youtube.com/embed/ZiIcqZoQQwg"; 
   UIWebView *wv = [[UIWebView alloc] init];
   wv.frame = CGRectMake(10, 80, width, height);

   NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
   [html appendString:@"<html><head>"];
   [html appendString:@"<style type=\"text/css\">"];
   [html appendString:@"body {"];
   [html appendString:@"background-color: black;"];
   [html appendString:@"color: white;"];
   [html appendString:@"}"];
   [html appendString:@"</style>"];
   [html appendString:@"</head><body style=\"margin:0\">"];
   [html appendFormat:@"<embed id=\"yt\" src=\"%@\"", youTubeURL];
   [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", 300.0f, 300.0f];
   [html appendString:@"</body></html>"];
   [wv loadHTMLString:html baseURL:nil];
   [self.view addSubview:wv];  
}
like image 261
Anoop K Avatar asked Jun 06 '13 06:06

Anoop K


2 Answers

What you will have to do is like below...

First make one notification in viewdidLoad which will be called when you rotate your device..

-(void)ViewDidLoad
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(rotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];
}

now the notification will call method named "rotate:" so we will have to implement that method like below.

#pragma mark - Rotate Screen Method
- (void)rotate:(NSNotification *)n {

switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationLandscapeLeft:
        yourwebview.transform = CGAffineTransformMakeRotation(M_PI / 2);
        yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame
        break;
    case UIDeviceOrientationLandscapeRight:
        yourwebview.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        yourwebview.frame = CGRectMake(0, 0,768, 1024);//you can set any frame
        break;
    case UIDeviceOrientationPortrait:
        yourwebview.transform = CGAffineTransformIdentity;
        yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        yourwebview.transform = CGAffineTransformMakeRotation(M_PI);
        yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame
        break;
    default:
        break;
   }
 }

and that's it.

like image 141
NiravPatel Avatar answered Sep 20 '22 05:09

NiravPatel


If you need both portrait and landscape support, you will have to set the app's supported modes under "Target" -> Summary -> "supported interface orientations".

Your code to load the player is correct. You just have to add the orientation support alone.

If you are using auto layout, check the constraints in xib. If not, then set frames for web view on each orientation change as mentioned by NiravPatel.

If you want only one view controller to rotate, add this in all view controllers:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr‌​ientation { return NO; }

You can return YES on the Viewcontroller that should rotate and return NO on other viewControllers.

like image 29
BrianChristo Avatar answered Sep 20 '22 05:09

BrianChristo