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.
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;
}
In my app i am using UITextView to display three lines. In that i need to add link for certain text. I am searched in google and SO, but i couldn't found the exact solution for my problem.
I Tried this but its not working
NSString *testString = #"This will go to Google";
[webview loadHTMLString:testString baseURL:baseURL];
[lblLinksTitle setText:testString];
I want Google as clickable text which have to open in safari browser. May be its simple. As i am new to iOS i couldn't figure out to do this.
Thanks for your help guys.
Much appreciated.
Just use UIWebView, don't use UiTextView
NSString *testString = #"This will go to Google";
[webview loadHTMLString:testString baseURL:nil];
webview.delegate=self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString * requestString=[[request URL] absoluteString];
//NSLog(#"%# is requestString from clicking",requestString );
if ([[[request URL] absoluteString] hasPrefix:#"http://www.google"]) {
[[UIApplication sharedApplication] openURL:[request URL]];
}
return TRUE;
}
Try using this:
lblLinksTitle.editable = NO;
lblLinksTitle.dataDetectorTypes = UIDataDetectorTypeLink;
lblLinksTitle.text = testString;
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...)
I have some local html files in resources.on launch one html,say a.html, is loaded in webview. there are some hyperlinks in that webview(or say the html content) which are links to other html files in my resources.now on click of those links I want to open them in in-app browser.
how to do this.
Any idea????
thnx!!!!
I've not tried this, but it might work!
Create your UIWebView and load the first html file from your bundle
NSString *file = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"html"];
NSString *html = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:[NSString stringWithFormat:#"<html><head></head><body>%#</body></html>", html] baseURL:baseURL];
webView.delegate = self;
Then get when the user taps a link
-(BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
// Find which link is selected, get the file and load it into the UIWebView
}
return YES;
}
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;
}