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
Related
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.
I'm trying to implement a MPMedia player to play internet files. So far I have managed to open the media player with links to videos hosted by me. The problem starts when I try to load an url that doesn't directly map to a file but instead gets redirected to the file.
The initial url looks like this: h*tp://www.example.com/numbers/numbers/numbers
and when I put it in my browser it is automatically changed for something like this: h*tp://www.example.com/numbers.mp4?to=numbers
When I use the initial url directly, just like I was doing with direct urls, the player comes up and inmediatly goes down without playing the video.
I have tried NSURLConnection to get the redirect url but I havenĀ“t been able to get it working, somehow the method - connection:willSendRequest:redirectResponse: doesn't get called. I think it may be because it has been removed in iOS 5, but I don't know any alternatives, and this kind of problems are really undocumented.
Here is my code: (I've skipped the shortUrl initialization, it works).
NSLog(#"%#",shortUrl);
redirecionando=TRUE;
NSURLConnection * conection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:shortUrl] delegate:self];
[conection start];
while(self.redirecting);
self.moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:finalurl];
moviePlayerController.view.frame = self.view.bounds;
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayerController.moviePlayer.shouldAutoplay = YES;
[moviePlayerController.moviePlayer prepareToPlay];
moviePlayerController.moviePlayer.fullscreen=YES;
}
- (NSURLRequest *)connection: (NSURLConnection *)inConnection
willSendRequest: (NSURLRequest *)inRequest
redirectResponse: (NSURLResponse *)inRedirectResponse;
{
finalurl=inRequest.URL;
self.redirecting=FALSE;
return inRequest;
}
I know that this last methos isn't getting called, I NSLogged it. Right now, when the above code gets executed it just waits because of the while loop.
By the way, I cannot use an external api to discover the redirection like http://longurl.org/ because the link returns a token access for the device that asks for
it.
Thanks in advance.
I solved it. It turs out that with the line:
while(self.redirecting);
I was bloking the main thread and thus preventing the method connection: willSendRequest: redirectResponse: from being called. I've solved it getting the code below that line and putting it into another function called from the redirect method. I've also had to modify the redirect method because it got called many times, even with the original url before arriving at the mp4.
Thanks if you took the time to read the question.
I'm trying to listen for an event (specifically a button click) from a webpage I have opened in a UIWebView in my iOS app. I know that in other languages there are ways to attach event listeners to certain web calls, but I haven't yet found a way to do it in Objective-C. I also haven't found anyone online who says it can't be done, so I decided I should ask. I saw in the documentation that you can make Objective-C calls from Javascript, but I do not have control of the webpage I am wanting to monitor. So I need a solution that allows me to listen for this event entirely in Objective-C.
EDIT: If specifics would help, I am trying to allow the user to make a wallpost on Facebook. I am loading the Facebook sharer page in a UIWebview (http://www.facebook.com/sharer/sharer.php?u=) and wanting to monitor when the user clicks "Share Link" so that I can close the web view.
Thank you all for your time!
You can use the UIWebViewDelegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
The UIWebViewNavigationType values are :
enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
};typedef NSUInteger UIWebViewNavigationType;
Can check this and then look at the NSURLRequest to get info about that
Use a custom scheme. When the user taps the button, ping a url with the custom scheme (myScheme://buttonTapped) and either:
Catch this in the webview delegate method shouldStartLoadWithRequest... (ie check if the URL contains your custom scheme) and route it to the appropriate objective c selector. Or
Register a custom URL protocol and set it up to handle your custom URL scheme. Something like the below:
#implementation MyURLProtocol
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
return [[[request URL] scheme] isEqualToString:#"myScheme"];
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
- (void)startLoading
{
NSURLRequest * request = [self request];
id client = [self client];
NSData * data = [NSMutableData dataWithCapacity:0];
NSHTTPURLResponse * response = [[[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:#"HTTP/1.1" headerFields:nil] autorelease];
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[client URLProtocol:self didLoadData:data];
[client URLProtocolDidFinishLoading:self];
[[NSNotificationCenter defaultCenter] postNotificationName:kSchemeNotification object:nil userInfo:payload];
}
- (void)stopLoading
{
}
I am trying to create a simple ipad app with a UIWebview that displays a form that a customer can fill in.. what i want to do is restrict the app so that it only allows the user to navigate to certain addresses.. (i.e. either something that allows the user to go to a specific address.. OR something that checks for specific keywords and allows/blocks them as appropriate..)
Could someone please show me how its done..
NB: its basically a googledocs form and i dont want to let the user navigate away from it.. (the user could easily click away and go elsewhere)
Thank you for reading :)
In the class that is your UIWebViewDelegate you can use something like this:
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//Check for your own url. You can use more advanced checking techniques of course :)
NSRange range = [urlString rangeOfString:#"http://www.yourUrl.com"];
if (range.location != NSNotFound)
return YES;
else
return NO;
}
You can use the delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
to determine whether the UIWebView can load a given web page. Although that would mean knowing exactly which pages are allowed (if there are many this might not be convenient).
Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:
Then check what is the URL that the UIWebView tires to load, and act consequently.
UIWebViewDelegate Reference
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;
}