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;
}
Related
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];
NSString *testString =[NSString stringWithFormat:#" Google"];
[webView loadHTMLString:testString baseURL:nil];
I want to get the URL when I click on this UIWebView content in
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:inRequest.mainDocumentURL];
return NO;
}
return YES;
}
delegate method.
When I click google hyperlink in webview it gives
URL: applewebdata://
Please help me.
You question is not very clear if you want to get the tapped url on webview or the webview's loaded page url ,
If you want to get webview loaded page url you can use NSURLRequestObject,something like
NSString * url = [request URL];
If you want to get the clicked link url , you will have to use this java script
NSString *url = [webView stringByEvaluatingJavaScriptFromString:#"window.location"];
you can do like this way
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString rangeOfString:#"www.google.com"].location!=NSNotFound)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}
For Swift:
Whenever your webpage is loaded you can use this, I use it for a label in this case:
webUrlLabel.text = webView.request.URL.absoluteString
For Objective-C:
NSString *currentPage = [NSString stringWithFormat:#"%#", webView.request.URL.absoluteString];
webUrlLabel is a UILabel
webView is my UIWebView
You already have NSURLRequest object. Access url from that.
inRequest.URL
way 1:
NSString *currentURL = myWebView.request.URL.absoluteString;
Way 2:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
yourTextBox.text = [url absoluteString];
return YES;
}
Please let me know if it works. Thanks
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
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];
I am building an app in xcode using the Cordova/phonegap framework for ios which displays some html that has some embeded youtube player code in it. iOS seems to redirect the user to the youtube app when it hits this youtube player. In cordova 1.5.0 the following code worked, but in 1.6.1 it doesn't seem to. Any ideas why or what needs changing to get it to work?
code to stop youtube opening up and links to behave selves
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
NSString* urlString = [url absoluteString];
if([urlString rangeOfString:#"http://www.youtube.com/embed"].location != NSNotFound) {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
else if (([[url scheme] isEqualToString:#"http"] || [[url scheme] isEqualToString:#"https"])) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
I would put logging in for url at the beginning of the method, and then a log statement in each clause of the if/elseif/else so you can see what urls are being intercepted and what the method then does with each one.
Maybe the string for the youtube request doesn't match the hardcoded "http://www.youtube.com/embed" any more? Worth a look.
look at http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
this might help.
It says to do this:
NSString *htmlString = #"<html><head>
<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>
<body style=\"background:#F00;margin-top:0px;margin-left:0px\">
<div><object width=\"212\" height=\"172\">
<param name=\"movie\" value=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"
type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"172\"></embed>
</object></div></body></html>";
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.your-url.com"]];