WebView shouldStartLoadWithRequest, get only the main url - objective-c

I have this code who get the url which is being opened by WebView:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSlog(#"My Url -> %#",request.URL);
}
this code sends me the complete url such as:
https://www.google.com/?gws_rd=ssl
In my case I just wanted to try to get the main url of this page, returning:
www.google.com
How could I do this?

request.URL.host should do the trick. For more info see here: https://developer.apple.com/library/Mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURL/host.

Related

WKWebView - Get query parameters of html form

I've just made the switch from UIWebView to WKWebView.
I have an HTML file (loaded as string) which includes an HTML form with one input with type text, and a submit button.
Using UIWebView, the URL included the value of the input on the URL as a query parameter. (some-url.index?key=value)
When switched to WKWebView, the URL does not include the parameters as part of the URL.
In UIWebView I was getting the URL like so:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"Loading URL :%#",request.URL.absoluteString);
}
In WKWebView I'm getting the URL like so:
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(#"Loading URL :%#", navigationAction.request.URL.absoluteString);
}
Thank you all for the help.
For other users that encounter this issue in the future:
After digging a bit, it seems that in WKWebView in order to have query parameters passed in the url, the url must have a valid scheme (http://, https://, etc..)
All other ways failed for me.

UIWebView - shouldStartLoadWithRequest - NSMutableURLRequest

I have an UIWebView embeded in my app. What I am doing is, add an header (to be specific, authorization header), to all requests made from it.
This event is fired when a URL opens from an HTML iframe BUT it does not seem to be able to add header to that request.
To be sure, I added a log entry in this event and it does well but if I monitor HTTP traffic using burpsuite, it's not adding header.
Has anybody encountered such issue ever? Any possible workaround?
Below is my code, if it helps.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
[(NSMutableURLRequest *)request addBasicAuth:self.accountObj.Username andPassword:self.accountObj.Password];
NSLog(#"!Request URL :%#",[request URL]);
NSLog(#"!Request Header :%#",[request allHTTPHeaderFields]);
return YES;
}
Instead of adding a header to your actual request, why dont you create a new one?
Cancel that one, and throw a new one

Not able to access request object of UIWebView inside webViewDidStartLoad

I am trying to access request object of UIWebView in webViewDidStartLoad in the following manner:
- (void) webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"req : %#", [webView request]);
-----
-----
}
It displays following in console :
req : (null)
Is the above possible or I am doing something wrong?
down vote
I donot want to use it inside webView:shouldStartLoadWithRequest:)request navigationType: as this method does not get called always for goBack and goForward methods
Looks like you will still have to consider shouldStartLoadWithRequest.
Did some testing and it looks like the [webView request] is 'one step behind' in delegate methods.
This was the code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"req1a : %#", [request URL]);
NSLog(#"req1b : %#", [[webView request] URL]);
return YES;
}
- (void) webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"req2 : %#", [[webView request] URL]);
}
And this were the results for initial (about:blank) and after click (some video stream, not actual printout) request:
initial request:
req1a : about:blank
req1b : (null)
req2 :
after click to a link:
req1a : xttp://origin.biokoda.tv/...
req1b : about:blank
req2 : about:blank
It looks like [webview request] is holding last loaded request.

UIWebView & ShouldStartLoadWithRequest

I have set up an app that uses a UIWebView. The UIWebView is used to display text formatted with CSS. In the text are links (a href=...). The format of the href is like this: tags.tagname
When a user clicks a link, I'd like to intercept the request and use loadHTMLString based on the contents of the HREF.
I have successfully parsed the URL, but I'm stymied on how to tell the webview to not load the requested href and load the string I want instead.
I've tried the following within ShouldStartLoadWithRequest:
genHTML=[self genTagPage:parsedString]; // a string declared earlier
[self.noteHTML loadHTMLString:genHTML baseURL:nil];
return NO;
But when the code is executed, I receive the following error:
2011-04-19 22:39:21.088 TestApp[27026:207] *** -[_PFArray release]: message sent to deallocated instance 0xaa3cf40
genHTML is getting populated with the right data, so I'm at a loss as to how to proceed.
Thanks.
It sounds like you just need to prevent the default action for the link like the following:
<a href="somewhere.html" onClick="doSomething(); return false">
Check out quirksmode for more details.
Edit:
Re-read your original code, and it would seem you've got an infinite loop going on. shouldStartLoadWithRequest gets called once when they click on the link, then infinitely on your loadHTMLString line. Making the call conditional on the navigationType should fix your problem:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked)
{
genHTML=[self genTagPage:parsedString]; // a string declared earlier
[self.noteHTML loadHTMLString:genHTML baseURL:nil];
return NO;
}
return YES;
}

loading different page using webView: decidePolicyForNavigationAction: request: frame: decisionListener: method

Hii All,
I want to load a new page when following method is called....I am using the following code..
(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id )listener
{
[[myWebView mainFrame] loadRequest:someRequest];
}
but this method is called multiple times and my application crashes if i use [listener use] instead of loadRequest it works fine but launches the url clicked . but i want to load some other url how is it possible?
You should simply add [listener ignore] method call.
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id )listener
{
[listener ignore];
[[myWebView mainFrame] loadRequest:someRequest];
}
It's called multiple times as you say, so you have to pay attention to this:
[actionInformation valueForKey: #"WebActionNavigationTypeKey"]
That value should be one of the WebNavigationType enum:
WebNavigationTypeLinkClicked,
WebNavigationTypeFormSubmitted,
WebNavigationTypeBackForward,
WebNavigationTypeReload,
WebNavigationTypeFormResubmitted,
WebNavigationTypeOther
You will get WebNavigationTypeLinkClicked first as a result of a link clicked, and here you can decide whether to load the page clicked or something else.
Immediately after you get WebNavigationTypeOther which is the page load, and you can ignore it.
Well, in this method it is mandatory to return a value.
You're supposed to decide if you accept or not this url.
So if the WebView is going to a page you don't want,
you should return ignore.
And have the webView go to your other url: [myWebView setMainFrameURL:otherUrlStr]
Thanks