Ian Dundas
Ian Dundas

Reputation: 572

Forcing portrait mode when playing YouTube video in UIWebView

edit: The videos are of the app being used: i.e. they're instructional videos designed to be played in portrait mode.

I used this method (NB iPhoneDevSDK.com is currently marked by Google as containing malware, so be careful. I've uploaded a screengrab of the relevant post) to open & play a YouTube video (using a UIWebView behind the scenes).

The video I want to show was recorded in portrait (uploaded in 320x480) and was intended to fill the iPhone screen nicely when in portrait mode. However, the YouTube video starts in a forced landscape mode, and can only be put into portrait by moving the phone to a landscape position and then back again (sometimes needs to be done a few times).

What I'm wondering is: is there a way of forcing the UIWebView to open a video player that defaults to portrait mode?

NB I've set (interfaceOrientation == UIInterfaceOrientationPortrait) on the UIWebView delegate that's spawning the UIWebView.

Edit: I even tried subclassing UIWebView on the off-chance that this would get it to work:

@interface AltWebView : UIWebView
@end

@implementation AltWebView

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

In Landscape by default In Portrait mode once rotated a few times

Upvotes: 1

Views: 4006

Answers (2)

torutoru
torutoru

Reputation: 131

I've been facing same problem recently and used this code. This may not be the right answer to your question but it plays YouTube video in portrait mode.

NSString *embedHTML = @"<iframe title=\"YouTube video player\" width=\"320\" height=\"460\" scrolling=\"no\" src=\"http://www.youtube.com/embed/%@?modestbranding=1&amp;rel=0;autoplay=1;showinfo=0;loop=1;autohide=1\" frameborder=\"0\" allowfullscreen allowTransparency=\"true\"></iframe>"; 

NSString *htmlStr = [NSString stringWithFormat:embedHTML, videoID];  // videoID is like EgXdUs9HsrQ...  

[webView loadHTMLString:htmlStr baseURL:nil]; 

Upvotes: 4

Michael Dautermann
Michael Dautermann

Reputation: 89509

For whichever view controller that owns the UIWebView, you could specify that you only want to display everything from that view controller in portrait mode.

Something like, say:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

and

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

B.T.W., there's a helpful question with lots of helpful hints over at: iPhone SDK: Orientation (Landscape and Portrait views)

Upvotes: 1

Related Questions