Obj-C - Embed YouTube video in UIWebView? - objective-c

I'm using the below code to try and have a youtube video play inside my app (in a UIWebView). For some reason, my WebView returns as just a black box, and no video plays.
See below:
DashboardViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSString *videoURL = #"https://youtu.be/8QrCPihtBSc";
UIWebView *videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.howtoView.frame.size.width, self.howtoView.frame.size.height)];
self.howtoView.backgroundColor = [UIColor whiteColor];
self.howtoView.opaque = NO;
self.howtoView.delegate = self;
[self.howtoView addSubview:videoView];
NSString *videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"100%%\" height=\"240px\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>", videoURL];
[videoView loadHTMLString:videoHTML baseURL:nil];

UIWebView is already deprecated, please us WKWebView
There is embed button on youtube web page, that gives you required code.

Related

WebView embedded video play/pause options are not appreared on tap in iOS 8.0

I had embedded video in webview which is loaded from app bundle but now i am having one issue in iOS 8.0.
I am not able to see video play/pause/stop and zoom options panel when i tap on it. I can make it fullscreen by pinch gesture and able to see option panel in full screen but not able to see it while playing normally.
It seems that uiwebview content is not in center or may be its content size is increased. I tried to found its solution online but couldn't found any solution for that. I tried everything which i found about embedding video in uiwebview but the issues is not resolved yet. I think the playback option is appeared but as content size is increased so i think that playback option is being visible out of bound. This issue only occurs in iOS 8.0 and it works fine in ioS 6.0 & 7.0.
Code which i have done to integrate it:
webviewFrame = CGRectMake(335, 67, 675, 670);
webview = [[UIWebView alloc] initWithFrame:webviewFrame];
[webview setBackgroundColor:[UIColor clearColor]];
[webview setOpaque:NO];
webview.delegate=self;
webview.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[webview setScalesPageToFit:YES];
[webview.layer setMasksToBounds:YES];
[webview.layer setCornerRadius:15.0f];
[webview.layer setBorderColor:[UIColor grayColor].CGColor];
[webview.layer setBorderWidth:2.0];
[self.view addSubview:webview];
webview.hidden=NO;
webview.delegate=self;
btn_Zoom.hidden=YES;
btnMail.hidden=YES;
UIScrollView* sv = (UIScrollView*)[webview.subviews objectAtIndex:0];
sv.scrollEnabled=NO;
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:strPath isDirectory:YES]]];
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"finish loading");
[appDelegate hideIndicator];
self.view.userInteractionEnabled=YES;
CGSize contentSize = CGSizeMake(webView.frame.size.width,
webView.frame.size.height);
webview.scrollView.contentSize = contentSize;
[webview setScalesPageToFit:YES];
self.automaticallyAdjustsScrollViewInsets = YES;
self.edgesForExtendedLayout = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"error=%#",error);
[appDelegate hideIndicator];
self.view.userInteractionEnabled=YES;
CGSize contentSize = CGSizeMake(webView.frame.size.width,
webView.frame.size.height);
webview.scrollView.contentSize = contentSize;
[webview setScalesPageToFit:YES];
self.automaticallyAdjustsScrollViewInsets = YES;
self.edgesForExtendedLayout = YES;
}
Please find me accurate solution of this issue as i am trying to solve it from last 2 days.

Issue with not getting sound in device when play youtube video in UIWebView in IOS

I am using a UIWebView to play Yoytube Video.
My code is like that
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,320, 240)];
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setOpaque:NO];
self.webView.delegate=self;
[self.webView setAllowsInlineMediaPlayback:YES];
[self.webView setMediaPlaybackRequiresUserAction:NO];
[self.viewContent addSubview:self.webView];
NSString* embedHTML = [NSString stringWithFormat:#"\
<html>\
<body style='margin:0px;padding:0px;'>\
<script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\
<script type='text/javascript'>\
function onYouTubeIframeAPIReady()\
{\
ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
}\
function onPlayerReady(a)\
{ \
a.target.playVideo(); \
}\
</script>\
<iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%#?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
</body>\
</html>", 320, 240, [dictIW valueForKey:#"embedCode"]];
[self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
In Simulator it is playing absolutely fine. But when I play this video in iPhone or iPad, the video is showing, but the sound is not getting.
please help.
Set the audio session category to 'playback' before creating the UIWebView
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL flag;
NSError *setCategoryError = nil;
flag = [audioSession setCategory:AVAudioSessionCategoryPlayback
error:&setCategoryError];
do{
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback, options: .defaultToSpeaker)
} catch let error {
print("Error: \(error)")
}
in your view did load, if using swift.

Javascript return a string to objective c

i got a webview and i would like to capture an input from a webview HTML form. i have tried but i cant can some one help me please
this is my webview code
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
[webView release];
return self;
To get value from webView, i used below code.
NSString* value = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('text').value"]‌​;
NSLog(#"got: %#",value);
You could look into how phonegap http://phonegap.com/ solves this.

After YouTube video play app doesn't come back in the same position

So I managed to play an embeded youtube video in my app. After I press 'done' the app doesn't come back in the same position where it was before playing the video. Please help me with this. This is the way I do it.
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame
{
NSString *embedHTML = #"\
<html><head>\
style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
videoView.center=CGPointMake(160, 240);
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
My problem was that I had a view which I presented modally. In this case, when youtube goes away and your app comes back to foreground it won't come back in the same position, but in the position before presenting the modal. So I solved my problem by adding that view as a subview rather than presenting it modally.

How to show HTML5 enabled videos in uiwebview?

I have to show HTML5 enabled videos in my iphone application using the uiwebview. How to resolve this problem? Any one having idea please give with example code.
My code to show the video:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Movie-1" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movie_obj = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[movie_obj.view setFrame:CGRectMake(0,0,320,460)];
movie_obj.scalingMode=MPMovieScalingModeAspectFit;
[self.view addSubview:movie_obj.view];
[movie_obj play];
I'm quite sure it should support this by default. The main problem would be the HTML part.
But what doesn't work? Because seeing your tags, i think you're not the one making the site, but the UIWebView simply won't show the video?
Edit: check out the post UIWebView to Play Local and downloaded Video or, more specifically, this answer:
Solution is here, you can play video in Embedded UIWebView.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4\" type=\"application/x-shockwave-mp4\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 412.0)];
[webView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}