Youtube in UiWebView - ios

I'm getting the URL of the youtube video with this:
NSString *currentYouTubeVid = [youTubeWebView stringByEvaluatingJavaScriptFromString:#"function getURL() {var player = document.getElementById('player'); var video = player.getElementsByTagName('video')[0]; return video.getAttribute('src');} getURL();"];
This give me this address:
http://r5---sn-uxanug5-coxe.googlevideo.com/videoplayback?sparams=id%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=907050%2C930813%2C916807%2C916611%2C936910%2C936913%2C907231%2C921090&app=youtube_mobile&source=youtube&key=yt5&itag=18&upn=Z8Hh3zpeVwM&ms=au&signature=1E59024805944173684C36996EDB75B8523ADF74.D1BD60DB4276262A08B00FB3A6CF3CFD56B95754&ratebypass=yes&ipbits=0&expire=1389929407&dnc=1&id=7ee5d672b87516cc&mt=1389909046&sver=3&yms=iPABvX0BA-A&mv=m&el=watch&ip=124.191.169.135&cpn=k3p_cmT1D3NyIEk
Is there any way to shorten this address and get something like:
http://www.youtube.com/watch?v=fuXWcrh1Fsw
Thanks if you can help.

After the UIWebView content is loaded, you can view/get its URL with:
NSLog(#"WEB VIEW URL: %#", youTubeWebView.request.URL.absoluteString);
The result I get in the console is:
2014-01-16 17:37:07.014 WebViewer[77010:70b] WEB VIEW URL: http://m.youtube.com/watch?v=IKi0C22S5qo
The reason you are seeing the long extended string with your call is due to the fact that the site is actually triggering more URL loads after the original page is loaded. I tested the code above in the - (void)webViewDidFinishLoad:(UIWebView *)webView method. That method gets called multiple times on the same page whenever a new URL is loaded, despite the fact that you are still on the same page.
Regardless of the fact that it is called multiple times, that youTubeWebView.request.URL.absoluteString remains the same.

Related

Detect when url containing local anchor tag is clicked on UIWebView?

I want to detect when a url with local anchor tag is clicked on html page which is current been rendered on UIWebView. I have done lots of searches around and implemented various workarounds but no success. The ultimate suggestion from all around always comes to this delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
The biggest problem what I concluded was that this method simply doesn't get called for local anchor tags if already in a rendered HTML page. What i mean is if fresh new page URL (anchor tag) included is clicked that method gets fired. Here is an example. If i my UIWebView has already rendered www.xyz.com, now if i press some url which is like www.xyz.com#amazing (link present on that same page) then delegate method doesn't get fired.
I was developing a simple in-app browser. I had to show URL of every page been rendered/visible at address box of my in-app browser. Only obstacle now is i cannot show every anchor tags/javascript links that just brings new content but same URL domain.
There were solutions regarding usage of this
`[webView stringByEvaluatingJavaScriptFromString:#"window.location.hash"]`
Problem is that until and unless if I can't get delegate method called/fired than there is no benefit of this code.
Is there any workaround or shall I just give up on this? I hope I am clear with explaining the problem.
Note: Best TestCase Scenario is mobile version pinterest.com on UIWebView
Thanks.
I don't exactly follow what you're trying to do, perhaps you could clarify a bit more.
In the meantime, here's how to get your UIWebView to handle every link click in the web page it has loaded. It's looking for a URL Scheme that begins with "bloodwing". If it finds it, it will stop the request. Otherwise, it will work as expected.
So http://www.yahoo.com will work, and bloodwing://www.yahoo.com will fail.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([url.scheme isEqualToString: #"bloodwing"]) {
[webView stringByEvaluatingJavaScriptFromString: #"doSomething();"];
return NO;
} else {
return YES; // Return YES to make sure regular navigation works as expected.
}
}
Hopefully this helps!

After attempting to create a UIWebView with a GIF in it, when webViewDidFinishLoad is called, the URL is always just about:blank. Why?

I'm trying to show a UIWebView with a GIF in it, but only once the GIF has loaded.
I load the GIF as follows:
self.GIFWebView = [[UIWebView alloc] init];
self.GIFWebView.delegate = self;
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
[self.GIFWebView loadHTMLString:html baseURL:nil];
Where post is just an object with some properties such as the URL for the GIF.
Then in webViewDidFinishLoad: I show the web view:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%f", webView.scrollView.frame.size.width);
NSLog(#"%#", [webView.request.URL absoluteString]);
}
I get "0" and "about:blank" for the NSLogs each time, however.
Why does it not load the GIF properly?
I get "0" and "about:blank" for the NSLogs each time, however.
Not surprising. You're telling the web view to load HTML that you're providing in a string rather than giving it a request. The URL that you're logging is the request URL, and since there's no request, there's no request URL.
Why does it not load the GIF properly?
Possibly because you're misusing the URL object. Look at the code:
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><img src=\"%#\"></body></html>", post.url];
We can't tell what type post.url is, but it's probably NSURL*. You're probably passing a NSURL into the format string, and that may not produce the result you're looking for. Try passing in a string like [post.url absoluteString] instead of the actual NSURL object.
Also, you might want to log the value of html right after you create it so that you can check the full HTML that you're sending to the web view.
Update: Some additional things to check:
Are you running the code in question on the main thread?
Is the thread's run loop getting time?
Have you tried setting a non-nil base URL?
If the web view's delegate has a -webView:shouldStartLoadWithRequest: method, does it return YES?
What happens if you use a constant string that includes the HTML you want and the hard-coded URL instead of constructing the string with +stringWithFormat:?
Is the test device connected to the network? (Sometimes it's the simplest thing that gets you.)
Does it work correctly if you use a different image? I notice that the URL you're using is for an animated .gif file, try a non-animated .gif or a .jpg image instead.
Update 2: The problem lies in your creation of the web view. Look at the very first line in the code that you showed:
self.GIFWebView = [[UIWebView alloc] init];
That looks okay for a typical object, but -init is not the designated initializer for a view. You should use -initWithFrame: instead. The image loads fine in your sample project when I change the code in your project to use the right initializer:
UIWebView *GIFWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];

Webview is not showing anything

I am trying to load UIWebView on detailview when users tap in masterView Table. But somehow my UIWebView is not showing anything.
Here is my code:
NSString *manualURL = self.detailItem;
NSURL *url = [NSURL URLWithString:manualURL];
[self.manualWebView loadRequest:[NSURLRequest requestWithURL:url]];
I have also tried to check the value of url in debugger by printobject (po url) its showing correct url address but webview is still empty.
I just found out that if I put whole address like http://en.wikipedia.org/wiki/london then its working but if I put anything like www.google.com then its not. Any explanation ?
My code was just fine the problem was with the url. I was pulling out urls from plist and in Plist they were stored as "www.xyz.com" and when I replaced them with "http://www.xyz.com" it worked fine. Still don't know why it behaves in that way.

GET UIWebView URL ! (myWebView.request.URL.absoluteString is not working)

I want to get the URL of my webview. I searched for solutions, the only one was
NSString *myUrl = webView.request.URL.absoluteString ;
This is not working anymore ! it's giving me (null) as a result.
I'm developing for iOS 5.
There is nothing wrong with the absoluteString method, but webView.request is probably nil as well. It seems that the request property of UIWebView is nil until the request has been loaded.
If it's not a problem to wait for the webview to complete the loading, use the delegate method -(void)webViewDidFinishLoad:(UIWebView *)webView to get your URL. If you need it before that, you should get it from another place in your code (for example, where the webview's loadRequest is being called).

How can I get a frame's content with mshtml?

Here's the issue:
I have a hook in IE that reacts on WebBrowser.OnNavigateComplete2 event to parse the content of the document for some precise info.
That document contains frames, so I look into the HTMLDocument.frames. For each one, I look into the document.body.outerHTML property to check for the content.
Problem is, the string I'm looking for never shows there, whereas it is displayed in the finale page. So, am I looking in the wrong place? If it is displayed when the page is fully loaded, then it's downloaded at some point, right? But in which object should I look ?
BTW, I Don't know if that is of any importance, but the page I'm searching into comes from a ASP.NET application.
public void OnNavigateComplete2(object pDisp, ref object url)
{
document = (HTMLDocument)webBrowser.Document;
mshtml.FramesCollection frames = document.frames;
for (int i = 0; i < frames.length; i++)
{
object refIdx = i;
IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref refIdx);
string frameContent = frame.document.body.outerHTML;
}
}
Thank your for your help.
#rams
This event is launched many times for each page, so I figured it was each time a framed is loaded, even if i don't get to catch the one I'm looking for. If not, what would be the event to catch the frames content?
What I want to do is detect some precise info on a precise frame, then save it. later, a web page is loaded triggered by some user action, where I need the info I got from parsing the frame.
Do you know the name/id of the frame you are looking for content? If so, in your navigateComplete2 event, can you get a reference to the frame like
iFrame frm = document.frames(<your frame id>);
int readyState=0;
while(frm.readystate !=4){
// do nothing. be careful to not create an endless loop
}
if(frm.readyState==4){
// get your content now
}
HTH
Are you using some kind of threading? Running the browser in a separate thread really messes up things. Try to execute it in an STAThread and check if you get the correct result.
The reason your string does not show is because of the frame. The web browser control fires the document navigate complete event after it has loaded the main document. At this point, the frames haven't yet requested their sources. After the document is parsed by the web browser control, requests for the frame sources are issues and downloaded.
Can you please describe what you are trying to accomplish?

Resources