Parsing of loadHTMLString when using stringWithFormat - iphone

Have a series of YouTube videos that I want to embed without having to rewrite the embed code each time, so have used stringWithFormat to dynamically insert the URL:
videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:100%; margin-top:-150px;}\
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];
When I log the result, it looks HTML fine, but it isn't rendered correctly in the simulator, the scale and position of the thumbnail is wrong. If I simply hardcode the URL into the string then it works fine. So I'm guessing it's a problem with stringWithFormat?
Any ideas?

When using -stringWithFormat:, you need to escape the % character literals in the string with another % character
NSString *htmlFormatString = [#"
<html>
<head>
<style type=\"text/css\">
iframe {position:absolute; top:100%; margin-top:-150px;}
body {background-color:#000; margin:0;}
</style>
</head>
<body>" stringByAppendingFormat:#"<iframe width=\"100%%\" height=\"240px\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe>
</body>
</html>", videoURL];
[videoView loadHTMLString:videoHTML baseURL:nil];

Related

uiwebview detect youtube video is loaded completeley

I am playing youtube video in uiwebview using following code
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.youtube.com/embed/%#?autoplay=0&showinfo=0&controls=0\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, videoToken, videoView.frame.size.width, videoView.frame.size.height];
[videoView loadHTMLString:html baseURL:nil];
My problem is that until the youtube video is not loaded I want to show loading or some thing to say it is being loaded. I tried to detect in webview did finish load, but it comes 2-3 times in that method.
Please help
If you want to detect video is loaded or not then use below method , it is delegate method of UIWebview
You must set delegate of UIWebview in your View Controller Class.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//your code here
}
Let me know it is working or not!!!
Happy Coding!!!!

filtering src attribute of iframe in ios

I have a website that allows the user to embed their videos using tags such as .
<iframe width="560" height="315" src="http://www.youtube.com/embed/se2P7hjPanE" frameborder="0" allowfullscreen></iframe>
actually this iframe string is coming from service like this
<br />
\n<iframe width="560" height="314''
src="http://www.youtube.com/embed/se2P7hjPanE" frameborder="0"
allowfullscreen></iframe>
how can i get my original iframe string how to parse the src value and may be other values also to load video actually i used MWfeedparser(https://github.com/mwaterfall/MWFeedParser) but iam not getting like this
<iframe width="560" height="314'' src="http://www.youtube.com/embed/se2P7hjPanE" frameborder="0" allowfullscreen></iframe>
after height attribute i got (' ')instead of "
how can i resolve this issue
i want to get src value from above frame i want to play that video in webview of iphone
so that i want to play that particular video (which the client uploaded) in my iphone .
Sample Code :
NSString *myString = #"<iframe width="560" height="314'' src="http://www.youtube.com" frameborder="0" allowfullscreen></iframe>";
NSRange firstRange = [myString rangeOfString:#"http://"];
NSRange secondRange = [[myString substringFromIndex:firstRange.location] rangeOfString:#"""];
NSRange finalRange = NSMakeRange(firstRange.location, secondRange.location);
NSString *subString = [myString substringWithRange:finalRange];
NSLog(#"subString :: %#",subString);

iphone UIWebView embed youtube video

I use this code in my app to embed youtube video
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: transparent;\
}\
</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];
The youtube video url is from urlString, like "http://www.youtube.com/v/QfWGRIlpNBE". And this code works fine.
My question is, if the video is in flash format, then it can not be played on iphone, and I got a slosh cross the play button.
How can I detect if youtube video is a flash format or H.264 format? I only have the url of the video.
If you need to know before you construct your HTML, I think the only way would be to use the YouTube API. You could query for the video in your question with the following:
http://gdata.youtube.com/feeds/api/videos?q=QfWGRIlpNBE&max-results=1&v=2&format=1
There are no results for that video ID in H.263.
See http://code.google.com/apis/youtube/2.0/reference.html#formatsp for API doc.
Otherwise, you could just use an <iframe>:
http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html

Video thumbnail image deleted after closing video ("Done" button"

I am working with mp4 videos hosted by my company, not YouTube.
I am using the following code to create a clickable thumbnail in a UIWebView:
videoButtonHTML = [videoButtonHTML stringByAppendingFormat:#"<body bgcolor=\"#000000\"><img src=\"%#\" width=\"120\" height=\"90\" border=\"0\" type=\"application/x-shockwave-flash\"/></body>", videoURL, self.imageLink];
[videoButtonWebview loadHTMLString:videoButtonHTML baseURL:nil];
This works fine at first. It creates the thumbnail and plays the video. The problem is that, when you close the video, the thumbnail image disappears. I still have the "blank" Quicktime play image. I really want to keep the image, though.
I've tried this both with and without the shockwave tag - same result. I found an embedYoutube function in a tutorial. It works great with YouTube videos, but not with mp4 videos. Here's that code, in case it helps:
- (void)embedYouTube1:(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 loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
Any idea what's going on?
Try saving the image as NSData, maybe to your NSUserDefaults (when the video is loaded), then you can display the thumb from defaults, instead of the video itself (when the video is not loaded).

How to load Google Maps Faster in iPhone

in my iPhone application I'm loading the driving directions to a WebView from a bundled html page (because i can't find a way to load it directly to the MapKit), but it taking too much time to load, is there any better way to do so? Like in the iPhone default map application
code :
NSString *filePathString = [[NSBundle mainBundle] pathForResource:#"drive_index" ofType:#"html"];
NSMutableString *html = [[NSMutableString alloc] initWithContentsOfFile: filePathString];
[html replaceOccurrencesOfString: #"varLocation1" withString:loc1
options: NSLiteralSearch range: NSMakeRange(0, [html length])];
[html replaceOccurrencesOfString: #"varLocation2" withString:loc2
options: NSLiteralSearch range: NSMakeRange(0, [html length])];
NSURL *aURL = [NSURL fileURLWithPath:filePathString];
[webView loadHTMLString:html baseURL:aURL];
and the html page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Map</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
type="text/javascript"></script>
<script type="text/javascript">
var directionsPanel;
var directions;
var location1="varLocation1";
var location2="varLocation2";
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(location1), 13);
directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
directions.load('from: ' + location1 +' to: '+ location2 );
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width:divice-width ; height:350px ; float:top; border: 1px solid black;"></div>
<div id="route" style="width:divice-width; border; 1px solid black;"></div>
</body>
</html>
Have a look at this answer:
Showing Driving Directions in MapKit
It says a way to use the Map Kit (which doesn't include driving directions) and overlay lines directly on top. Here's the site they reference:
http://spitzkoff.com/craig/?p=65
You should definitely see if you can switch to V3 of the Google Maps API. It was specifically designed to load quickly on mobile browsers. The only problem (and it's a big one) is that it doesn't support directions yet.