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

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).

Related

Why does shouldStartLoadWithRequest not load my /amp version in the webview, and does in safari?

I have an iOS app with a UIWebView in it.
In my webiste all my pages have an /amp version in it ( i.e www.website-example.com/pageX/amp )
In the shouldStartLoadWithRequest: method I do :url = [url URLByAppendingPathComponent:#"amp"]
if I do [[UIApplication sharedApplication] openURL:url];return false; (so it opens the url in Safari) it opens the good /amp version.
Otherwise, if I return false (after appending /amp), it opens the url in the webview, it doesn't open the /amp version, although the URL contains /amp.
edit: I'm adding Swift tag, because it's not a programming language problem, it's understanding shouldStartLoadWithRequest method.
What is the problem?
You can't append /amp to the url in shouldStartLoadWithRequest, /amp must be added and passed to the loadRequest method. It opens fine in safari because you are passing "good" url, but UIWebView is opening url before appending /amp, and in shouldStartLoadWithRequest you can't manipulate url.
You should get this code :
[webView loadRequest:[NSURLRequest requestWithURL:[NSString stringWithFormat:#"%#/amp", request.URL]]];
in the method :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
That would be help you .

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!

Youtube in UiWebView

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.

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.

Resources