Load youtube URL in Xcode 5 - ios

I have been looking at numerous tutorials to play youtube videos via the app. However they are producing a blank screen. According to comments on the tutorials this is common on xocde 5? Is there another way in which videos can be streamed? I am running it on a iphone and have connected the webview.
I am using :
.h
#interface detailViewController : UIViewController {
IBOutlet UIWebView *webView;
}
VIEW DID LOAD
NSURL *url = [NSURL URLWithString:#"<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/AmMF7hWrYZk\" frameborder=\"0\" allowfullscreen></iframe>"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];

UIWebView *web;
NSString *pageHTML = [[NSBundle mainBundle] pathForResource:#"url" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:pageHTML encoding:NSUTF8StringEncoding error:nil];
[web loadHTMLString:htmlString baseURL:nil];
the code in url.html is
<html>
<head></head>
<body>
<iframe src='http://player.vimeo.com/video/73198189?title=1&byline=0&portrait=0&loop=1' width='690' height='500' frameborder='10' webkitAllowFullScreen mozallowfullscreen allowFullScreen>myframe</iframe>
</body>
i have used this code for playing vimeo videos and it worked.. hope this helps you..

I had the same issue happen when I was using the YouTubeHelper Xcode libraries (https://developers.google.com/youtube/v3/guides/ios_youtube_helper). I found that the first time I added the YT* libraries into my project that I didn't select the "Create Folder References for any added folders" when adding the assets folder into my Xcode project. After removing the existing assets folder and following that step I was able to get the helper libraries to work within my project and play YouTube videos as the link above describes. Hope that helps.

I had implemented youtube videos using the HCYoutubeParser. Very nice and easy to use.

Consider letting the web view tell you why its not loading
Implement UIWebViewDelegate methods in your view controller , and attach the delegate to your view controller.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
may give you a very good hint.

Send you NSURLRequest request from
-(void)viewWillAppear:(BOOL)animated {
}
Here is my code, used in my app
NSString *youTubeID = [self extractYoutubeID:url];
NSString *embedHTML =[NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: #666666;\
padding:%f %f %f %f;\
color: blue;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe height=\"%f\" width=\"%f\" title=\"YouTube Video\" class=\"youtube-player\" src=\"http://www.youtube.com/embed/%#\" ></iframe>\
</body></html>",paddingTop,paddingRight,paddingBottom,paddingLeft,videoHeight,videoWidth,youTubeID];
[self.webView loadHTMLString:embedHTML baseURL:nil];
// extractYoutubeID
+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
//NSLog(#"youtube %#",youtubeURL);
NSError *error = NULL;
NSString *regexString = #"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])];
if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
{
NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];
return substringForFirstMatch;
}
return nil;
}

I had the same problem. You forgot to put "http://" in your youtube link.
Here's the correct code:
NSURL *url = [NSURL URLWithString:#"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/AmMF7hWrYZk\" frameborder=\"0\" allowfullscreen></iframe>"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];

Related

Can't load javascript in UIWebView

I'm trying to load a javascript file but it doesn't work.
My code :
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><script type=\"text/javascript\" src=\"http://mywebsite.com/file.js\"></script></body></html>"];
[_webView loadHTMLString:html baseURL:nil];
This code is supposed to display a video, but nothing happens.
This code displays correctly the video in Desktop browser, my JS is correct.
use url in baseURL
NSURL *base=[NSURL URLWithString:#"website.com"];
Do not use string description. Instead use:
[_webView loadHTMLString:html baseURL:nil];
Write following code in - webViewDidFinishLoad:
NSString * strJavaScript = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://mywebsite.com/file.js"] encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:strJavaScript];

The Google Maps Embed API must be used in an iframe in UIWebview

I am trying to load google maps on UIWebview and calling http://maps.google.com/?saddr=22.718019,75.884460&daddr=33.391823,-111.961731&zoom=10&output=embed but it is showing error "The Google Maps Embed API must be used in an iFrame in UIWebview" It was working before now it is not working.I am trying to load this URL in iframe like this
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f&zoom=10",[self.startLat floatValue], [self.startLon floatValue], [self.destLat floatValue],[self.destLon floatValue]];
NSString *embedHTML = [NSString stringWithFormat:#"<iframe width=\"300\" height=\"250\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe>" ,googleMapsURLString];
NSString *html = [NSString stringWithFormat:embedHTML];
but it is not working and giving some WebKitError.
Please Help..
Thanks!
Your implementation of iFrame worked for me. Here is my method:
-(void)showMapOnWebview:(UIWebView*)webview withUrl:(NSString*)urlAddress
{
//Create a URL object.
NSURL *url = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *embedHTML = [NSString stringWithFormat:#"<html><head><title>.</title><style>body,html,iframe{margin:0;padding:0;}</style></head><body><iframe width=\"%f\" height=\"%f\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe></body></html>" ,webview.frame.size.width,webview.frame.size.height, url];
[webview loadHTMLString:embedHTML baseURL:url];
}
Yes Google stopped calls with this parameter "output=embed" from working I think In April 2014. See https://forums.embarcadero.com/message.jspa?messageID=642868 for a workaround using the IFRAME method.
Steve

How to play secured Youtube url starts with " https://" in ios sdk?

I am working on youtube streaming and displaying youtube thumbnail image.
i used code to display thumbnails:
NSString *Videoidtsr =[NSString stringWithFormat:#"%#",[strArray objectAtIndex:i]] ;
NSLog(#"vidoe sr %#",Videoidtsr);
NSString *YouTibevideoId=[Videoidtsr bstringByReplacingOccurrencesOfString:#"http://www.youtube.com/watch?v=" withString:#""];
youtubeURLForThumb = [NSURL URLWithString:[NSString stringWithFormat:#"http://img.youtube.com/vi/%#/0.jpg",YouTibevideoId]];
NSLog(#"youtubeURL %#",youtubeURLForThumb);
Code for playing youtube video:
- (void)embedYouTube {
videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"100%%\" height=\"250px\" src=\"\%#\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>", videoURL];
[videoView loadHTMLString:videoHTML baseURL:nil];
}
i used url: http://www.youtube.com/watch?v=FQPJkXyn93c
It wokred fine
but this code is not working for another url: "https://www.youtube.com/watch?v=KULJOEkR0l4"
Since it is secured url consists "https"
How to play this type of urls also? and how to create thumbnails? can we paly secured urls in iphone or not?
Any answer can be appreciated.
Please help me in solving this.
Thanks
In your
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"is https scheme ? = %#",[[request URL] scheme]);
//same for if([[[request URL] scheme]isEqualToString:#"http"] ) {
if([[[request URL] scheme]isEqualToString:#"https"] ) {
//here you call the code / method to play video
return NO;
}
else return YES;
}

How to localize html strings in iOS

I want to write html code locally and show it in UIWebview ,so for this ,I have taken a simple html code ,as below and created a .html extension file..
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
I added this file into my xcode project ,now I want to load this file inside webview ,so I have written this code
[m_websiteView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]isDirectory:NO]]];
but the output I get when I run this code in UIWebView is same htmlcode, I cant figure out what the problem is..but when I tried using this code
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *testString = #"<h1><i>This is an image using reference in HTML</i></h1>"
"<img src='active.png'></img>";
[m_websiteView loadHTMLString:testString baseURL:baseURL];
this works perfect.also I want to localize the strings in this html code,so in the above code,how can I localize this string "This is an image using reference in HTML"..
Regards
Ranjit
Add localization, use this
NSString *testString = [NSString stringWithFormat:
#"<h1><i>%#</i></h1><img src='active.png'></img>",
NSLocalizedString(#"This is an image using reference in HTML", #"htmlText")];
and specify the localize strings in the file Localizable.strings in the correct language directory.
You can also localized entire (HTML) files with the locale directory scheme.
HTML syntax is
<html>
<head>
</head>
<body>
</body>
</html>
after that you can preview your html file using something like that in your view controller
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://www.istoselidas.gr";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];
just replace url with your html file

Run HTML5/CSS3 in UIWebView

I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com

Resources