How to show HTML5 enabled videos in uiwebview? - objective-c

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];
}

Related

Obj-C - Embed YouTube video in UIWebView?

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.

Display a .pdf in an iOS app without downloading it from the internet

I'm looking to package a .pdf file with my app so that users can view it on one of the app's tabbed UIViews. Most of the posts I've read explain how to display a .pdf that must be downloaded first from the internet. My question is: how do you access a .pdf that is packaged with the app and display it on a UI(Web)View (with no internet access/download required)?
EDIT: This snippet was the key (where "ReadFile.pdf" was .pdf added to the project):
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"ReadFile" ofType:#"pdf"];
This is what you should do:
– (void) displayLocalPdfInWebView
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"JaapSahib-Gurmukhi" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[window addSubview:webView];
[webView release];
}
Source: http://www.developerfeed.com/how-display-local-pdf-file-ios-uiwebview/
What this will essentially do is:
Create a CGRect to set the screen bounds
Allow autoresizing
Set the UIWebView to to a resource
Load the file into the UIWebView
Comment, and tell me how it goes!

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.

UIWebView sharp corners

I have an UIWebView with and using QuartzCore to add some style to this. But when i'm execute:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, self.view.bounds.size.width-20.0, self.view.bounds.size.height-30.0)];
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.layer.cornerRadius = 10;
webView.layer.borderColor = [UIColor whiteColor].CGColor;
webView.layer.borderWidth = 3.0f;
[self.view addSubview:webView];
I get an rounder View but content is sharp. There is a way to roundend corners in content in WebView
The content view is actually the scrollView of UIWebView, so
webView.scrollView.layer.cornerRadius = 10;
Agreed with #graver Answer but not completely Coz His working will not work on iphone 3g and iPod 2nd generation .
webView.layer.cornerRadius = 10;
[webView setClipsToBounds:YES];
ClipsToBound will simple resize all the subviews under webView.

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.