I have a UIWebView in which some parts of it have an <a href="" > <img src="" </a> in it. I have added a UITapGesture to this UIWebView and so what I want is basically when a user taps on an image, it doesn't bring you to the link pointed to it but rather do something. I am having issues in preventing the image to go to the hyperlink it's pointed to. Any idea? I've tried injecting javascript so that it changes the href to some fake link when the user taps the image and then on the:
- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
I checked the request url if that matches the dummy I have. However, this doesn't work, for some reason changing the href link using:
NSString *stripLink = [NSString stringWithFormat:#"document.elementFromPoint(%f, %f).parentNode.href=%#", pt.x, pt.y, #"http://www.fakeurl.com"];
if ([self stringByEvaluatingJavaScriptFromString:stripLink] != nil){
NSString *testHref = [NSString stringWithFormat:#"document.elementFromPoint(%f, %f).parentNode.href", pt.x, pt.y];
NSLog(#"FINAL HREF IS %#", [self stringByEvaluatingJavaScriptFromString:testHref]);
}
any other ideas?
If you have control over the HTML source (and it sounds like you do?), then you can change the href to use a custom URI scheme, e.g. photo:// then you can do the following:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:#"photo"])
{
// Custom Handling Goes Here
return NO; // Stops the link being executed by UIWebView
} else
return YES;
}
If, however, you cannot control the HTML source, then you may need to be slightly more devious. E.g. you could check the end of the path and see if it's an image (jpg, png etc...)
Related
I am making an iPhone app that has a bunch of "articles" with certain words that I want to link to other "articles". I need to display the articles in UITableViewCells.
If there is a way to do this without a web view, that's great.
If I need to use a web view, should I just make <a href> style links? How can I have it jump to the article without creating a bunch of actual html pages, just have it let my program know that they clicked on that link and then the program goes and finds the appropriate article from a plist or something?
I've create a test demo using UIWebView and it works fine.
Create two HTML files:home.html and first.html.
// home.html
Home page. first
// first.html
First page. home
//viewDidLoad
self.webView.delegate = self;
NSString *htmlFilePathString = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html"];
NSURL *htmlFileURL = [[NSURL alloc] initFileURLWithPath:htmlFilePathString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:htmlFileURL];
[self.webView loadRequest:request];
// webView's delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
I understand that you can get the current URL of a UIWebview via the following code:
NSString *urlString = [[[webView request] URL] absoluteString];
However, I've found that this only works for a webView that you have actually loaded.
At some point in my application, I'm doing this: [webView goBack] which will show the webView's previous page. However, calling the above line to get the URL doesnt reflect the actual page shown.
Is there any way to accomplish this?
Thank you!
It might be a safe approach to get the URL at the moment the page has been fully loaded :
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSURLRequest *currentRequest = [webView request];
NSURL *currentURL = [currentRequest URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
Does this work for you ?
Implement the delegate function
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Before every page is about to start loading in your webview (even the goback) will give a call in the above function and the request object give the url is going to load.
Note: return YES if you allow the webview to load the page else NO.
Hope this help you solve your problem.
I want to add custom button on webView. they should also should be there when i try anything in url.
how is it possible??
basically i want to put buttons on uiwebView and they are custom buttons
//edited code...
I am doing this...here link is appearing but method is not getting called...and there wasnot any error in your code ..:)
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *HTMLData = #"<htmlClick me!--></style><br><br>";
[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
and then
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// only do something if a link has been clicked...
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
// check if the url requests starts with our custom protocol:
if ([[[request URL] absoluteString] hasPrefix:#"button://"]) {
// Do custom code
return NO;
}
}
return YES;
}
You'll just have to use links and style them.
Something like:
Click me!
Have a look at http://www.cssbuttongenerator.com/, very easy to create your own button and let it generate the css code for you. You'll actualy have to click on the button create itself to generate the code.
Execute custom code by clicking on a link(button) in html
First of all you've got to conform to the UIWebViewDelegate protocol, and set the delegate accordingly.
Then implement shouldStartLoadWithRequest.
You button links should look like this:
Click me!
We are using a custom protocol we make up: button://.
Now implement shouldStartLoadWithRequest like this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// only do something if a link has been clicked...
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
// check if the url requests starts with our custom protocol:
if ([[[request URL] absoluteString] hasPrefix:#"button://"]) {
// Do custom code
return NO;
}
}
return YES;
}
That's it.
I am working on an assignment which requires me to open a HTML page in a webview in iPhone. I have a hyperlink which is shown as a string eg. "sometext.fileextensiontype". When this text is clicked it redirects to a link eg. http:www.someweblink.com/uyewihwefkbnamsjdfb".
I am able to intercept the above URL but I want to read the hyperlink string i.e. sometext.fileextensiontype.
you require to confirm with the UIWebViewDelegate protocol and provide implementation to the below shouldStartLoadWithRequest
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *myUrl = [request URL];
NSString* urlString = [myUrl absoluteString];
return YES;
}
My app has a UIWebView that shows a fair amount of content. For some of that content I would like to exit the app, and launch Safari to handle the web content rather than doing it in my UIWebView. Is there an url format that will explicitly launch Safari rather than loading a page in the UIWebView?
Obviously I can't use http:// since that just opens the url in place. Can I use safari:// or something of the sort?
EDIT: Apologies, I wasn't clear originally. I am looking for a solution that involves changing urls on pages without making modifications to my client. Hoping for a native Safari launching pattern along the lines of tel:// for the phone.
Sure. Then just have the UIWebView delegate do:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString * customScheme = #"safari";
NSURL * loadURL = [request URL]; //this is the url the user tapped
if ([[loadURL scheme] isEqual:customScheme]) {
//if the url starts with "safari://"
NSString * absoluteURL = [loadURL absoluteString];
//replace "safari" with "https"
absoluteURL = [absoluteURL stringByReplacingCharactersInRange:NSMakeRange(0,[customScheme length]) withString:#"https"];
NSURL * openURL = [NSURL URLWithString:absoluteURL];
//open the URL in MobileSafari
[[UIApplication sharedApplication] openURL:openURL];
//tell your UIWebView to ignore this request
return NO;
} else {
//this is not a safari:// url, so handle it normally
return YES;
}
}