0
votes

I'm making an iPad app that loads a youtube video. I was able to get a UIWebView to load a youtube video with some iframe code found online. I need to be able to rotate the iPad and not have the video cropped when rotated to landscape but at the same time both landscape and portrait width is full width of device. What do I put in my iframe? I've tried to use

"meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no" 

but it didn't work. I tried to place it in the iframe script (not between the but within . This cannot be right. Any ideas? Any help is greatly appreciated. Here's my code below:

.m

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize webView;

-(void)viewDidLoad
{

   [super viewDidLoad];
   [self embedYouTube];

   NSLog(@"frame:%@", NSStringFromCGRect(self.view.frame)); // prints frame:{{0, 0}, {768, 1004}}
   NSLog(@"bounds:%@", NSStringFromCGRect([[self view] bounds])); // prints frame:{{0, 0}, {768, 1004}}

}

-(void)embedYouTube{

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                         error:&setCategoryError];
    if (!ok) {
        NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
}


     NSString *embedHTML = @"<iframe height=\"700\" width=\"900\" src=\"http://www.youtube.com/embed/QK8mJJJvaes\" frameborder=\"0\" allowfullscreen></iframe>";


     NSString *html = [NSString stringWithFormat:embedHTML];

    [webView loadHTMLString:html baseURL:nil];
    [self.view addSubview:webView];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

in my AppDelegate.m file, I have:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// we support rotation in this view controller
return YES;
}

.xib

both view and webView have Autoresize subviews checked, orientation for view is portrait, size for view is none, both are mode center and webView scales to fit page.

1

1 Answers

1
votes

I ended up figuring out the exact dimensions to give Xcode with an aspect ratio calculator.

-(void)embedYouTube{

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
                     error:&setCategoryError];
    if (!ok) {
    NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }

    NSString *embedHTML = @"<iframe height=\"548\" width=\"975\" src=\"http://www.youtube.com/embed/QK8mJJJvaes\" frameborder=\"0\" allowfullscreen></iframe>";

    NSString *html = [NSString stringWithFormat:embedHTML];

   [webView loadHTMLString:html baseURL:nil];
   [self.view addSubview:webView];

}