Rich86man
Rich86man

Reputation: 6557

iOS embed UIWebView change with new youtube API

I'm starting to notice a change in the way that youtube videos are being loaded into UIWebViews and I wanted to know if this is behavior we should be expecting in the future and/or if we can replicate the previous functionality.

Comparison screenshot : Youtube video loaded into a webview

Old on the right, new on the left. The added youtube button allows users to leave the youtube video and go into the youtube web interface. I would like to be able to prevent the user from leaving the video being played.

I am currently using a category on UIWebView like this :

- (void)loadYouTubeEmbed:(NSString *)videoId
{    
    NSString* searchQuery = [NSString stringWithFormat:@"http://www.youtube.com/embed/%@?showinfo=0&loop=1&modestbranding=1&controls=0",videoId];

    searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:searchQuery]];
    [self loadRequest:request];
}

I've noticed that my query will respect either modestbranding=1 or showinfo=0 but not both at the same time. Will this change as the youtube redesign rolls out?

Upvotes: 2

Views: 878

Answers (1)

Alex Muller
Alex Muller

Reputation: 1565

When the Youtube video is loaded, and webView:shouldStartLoadWithRequest:navigationType: is hit, you should be able to filter out that link so it won't proceed.

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[request URL] absoluteString] isEqualToString:@"<URL String Youtube spits out when video selected>"]) {
        NSLog(@"Blocking YouTube...");
        return NO;
    } else {
        NSLog(@"Link is fine, continue...");
        return YES;
    }
}

Upvotes: 1

Related Questions