embedding youtube video in iphone app

2020-07-16 02:38发布

I am new to IOS application development. I've added the code for embedding Youtube video in my app by using UIWebview and the embed code from the youtube. But when I run the application in my simulator, the webview is simply blank. I don't see any video thumbnail or anything else. I heard youtube videos will not run on IPhone simulators but this link ("http://www.youtube.com/embed/36db4r3MsgU")shows that the video is playing perfectly in simulator. kindly look into this link and suggest me a solution.

NSString *code = @"<iframe width=\"640\" height=\"360\" src=\"http://www.youtube.com/embed/36db4r3MsgU?feature=player_detailpage\" frameborder=\"0\" allowfullscreen></iframe>";
[[self video]loadHTMLString:code baseURL:nil];

Thanks, Abilash.G

8条回答
劳资没心,怎么记你
2楼-- · 2020-07-16 02:59

Google provides a helper to embed and control youtube videos in iOS apps. Have a look at the documentation here: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

查看更多
三岁会撩人
3楼-- · 2020-07-16 02:59

It is too much easy in swift 4

let url = URL(string: "http://www.youtube.com/watch?v=fDXWW5vX-64")

let request = URLRequest(url: url!)

webView?.load(request)

查看更多
对你真心纯属浪费
4楼-- · 2020-07-16 03:01

No need to use the embed code. The standard UIWebView aimed at the youtube web address should work fine...

// URL from safari address bar. 
NSURL *url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=fDXWW5vX-64"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
查看更多
甜甜的少女心
5楼-- · 2020-07-16 03:07

// MY UIWebview is named "videoYoutube" in this example below // UIWebview size = 320x180 // This below will set the width to the guides ... or close to it and scale dependent on the device size.

    let bounds = UIScreen.mainScreen().bounds
    let width = bounds.size.width - 18
    let height = bounds.size.width / 1.8
    let frame = 0

    let youtubeVideolink = "https://www.youtube.com/embed/Rg6GLVUnnpM"

    let Code:NSString = "<iframe width=\(width) height=\(height) src=\(youtubeVideolink) frameborder=\(frame) allowfullscreen></iframe>"

    self.videoYoutube.loadHTMLString(Code as String, baseURL: nil)
查看更多
疯言疯语
6楼-- · 2020-07-16 03:09

For the first suggested answer i faced some issues like the whole youtube page has been loaded for fixing that i used some extra codes, the codes are

NSString *baseUrl = [NSString stringWithFormat:@"%@%@",@"http://www.youtube.com/watch?v=",embedCode];
    NSURL *url = [NSURL URLWithString:baseUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    videoWebView = [[UIWebView alloc] initWithFrame:imageview.frame];
    [videoWebView loadRequest:request];
    [videoWebView setNeedsLayout];
    [videoWebView setAllowsInlineMediaPlayback:YES];
    videoWebView.mediaPlaybackRequiresUserAction = YES;
    videoWebView.scrollView.scrollEnabled = NO;
    videoWebView.scrollView.bounces = NO;
    [self.contentView addSubview:videoWebView];
查看更多
疯言疯语
7楼-- · 2020-07-16 03:13

I have used this in my app. this doesn't use any webview

https://www.cocoacontrols.com/controls/hcyoutubeparser

OR

NSURL *urlOfYouTube = [NSURL URLWithString:@"http://www.youtube.com..."];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlOfYouTube];
[self.webView loadRequest:requestObj];
查看更多
登录 后发表回答