HTML "on click" event on ios - iphone

I am importing all tags of a HTML page to UITextView. but i have few on click events like mail and phone in this HTML tags. how can i detect that on click event and open it in UIWebView.

suppose are you displaying String in UIWebView like bellow But First you must configure your webView outlet and connect proper Delegate then put below code in to ViewWillApear:-
[webview loadHTMLString:[NSString stringWithFormat:#"%# Have an enquiry for %#? Click hear and we will be in touch with you.* {margin:0;padding:0; font-family:Arial;font-size:15px;text-align:justify}", yourStringDiscription,YourHyperlinkWord] baseURL:nil];
in Above script this line:-
:[NSString stringWithFormat:#"<html><body text=\"#000000\">%# <br>Have an enquiry for %#? Click hear
in first %# is for String of contain and second %# for your Hyparlink
Now
you can get click event of this webViewdelegate method
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
//Got even here put breck point
return NO;
}
return YES;
}
you can got click event in above delegate hope you undastood what i am saying and hope its help's you

When you on click events, do you simply mean that you want to make things like the email address, phone # etc to be tappable, such that tapping a phone # would open the Phone app etc?
If so, have you tried simply setting the UITextView's properties in Interface Builder > Attributes Inspector.
See my image below:

Related

HTML button to a native bridge iOS

I have an index.html page that has multiple buttons. I would like to be able to click on a button and it goes to the native side of the phone. To call it and do some action on the HTML page. I don't know how to do that?
In other words i would like to go from HTML to ios native actions and features, such as calling camera, gyro and so on.
You have to make some changes in your html page.
Use image instead of button.Then use anchor tag around image tag.
Set href as "btn://btn1" or what you want to identify your button.
Ex.
<img src="test.png" >
In ios side:
1.Assign delegate to your UIWebview.
2.Implement below method
//Method when bookmark button pressed
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.scheme isEqualToString:#"btn"])
{
//Enter your Code here
}
return YES;
}
using above method you can identify witch button is clicked and according to that you can perform any action you want.

How to detect the user click a hyper-link

My app will display some text, and I want to make the hyper-link be able to be clicked. I have some questions about this feature.
How do I parse the text to be aware this is a link?
Once a user click the link, I don't want the OS to switch to Safari and open the link, this is very bad because the user can not go back to my application. So I want to open the link within my application. As soon as the user click the link, my app will present a view modally to display the web content. Any advice would be appreciated.
You probably want to subclass UILabel. When you change the text, have it try to see if the text is a hyperlink, if it is, set it to enable user interaction and change the text color to blue. When the user taps on the link, send a message to the main view controller (Possibly through delegation) to open the link.
to display web content in your app, look into UIWebView.
If your text can be formatted as html with hyperlinks (<a> tags), you could use a UIWebView to display it.
The UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method is called when a user taps a link. Usually you would make your controller implement this method, and set the UIWebView's delegate to the controller.
You're going to want to check out a Github project called LRLinkableLabel.
It will auto-detect any URLs that are inside the .text property.
You can use it like so:
LRLinkableLabel *label = [[LRLinkableLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 20.0)];
label.delegate = self;
label.text = #"Check out http://dundermifflin.com to find some great paper deals!";
Then just make sure self implements this method:
- (void) linkableLabel:(LRLinkableLabel *)label clickedButton:(UIButton *)button forURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:url];
}
You can also use the linkColor and textColor properties to configure the appearance of the label. From this point you can use it just like any other UILabel.
Remember to set the delegate to nil when you're all done to make sure everything is all cleaned up.
Hope this helps.

Getting selected text when UIMenuController is shown

When the user selects some text in UITextView or UIWebView, UIMenuController is shown. Is there any way to access this selected text programmitically?
I need it because I want to add custom item to UIMenuController: 'Search' option which will be intended to search for selected text in database.
Is it possible without using 'Copy' item in order to copy the text to pasteboard and then getting it from UIPasteBoard with the next time? - I am not interested in such workaround.
What I did was to add this method to a UIWebView's category:
- (NSString *)selectedText {
return [self stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
}
After that, I can use [webView selectedText] to access the current selection.
Did you have a look at Apple's documentation.You can use the MenuItems property provided to create your custom UIMenuController.Have a look at the image below
and follow this
link for details.
You can also have a look at the sample code provided by Apple to understand that feature.
http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010139
In this sample code they have show "Email" in the UIMenuController.
So go ahead and code...All the best...
Cheers
To grab text from a PDF displayed in a UIWebView this will work
-(void)copiedString{
[[UIApplication sharedApplication] sendAction:#selector(copy:) to:nil from:self forEvent:nil];
NSString *copiedString = [UIPasteboard generalPasteboard].string;
NSLog(#"Copied String: %#",copiedString);
}

Multiple UIWebViewNavigationTypeBackForward not only false but make inferring the actual url impossible

I have a UIWebView and a UITextField for the url. Naturally, I want the textField to always show the current document url. This works fine for urls directly input in the field, but I also have some buttons attached to the view for reload, back, and forward.
So I've added all the UIWebViewDelegate methods to my controller, so it can listen to whenever the webView navigates and change the url in the textField as needed.
Here's how I'm using the shouldStartLoadWithRequest: method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"navigated via %d", navigationType);
//loads the user cares about
if ( navigationType == UIWebViewNavigationTypeLinkClicked
|| navigationType == UIWebViewNavigationTypeBackForward ) {
//URL setting
[self setUrlQuietly:request.URL];
}
return YES;
}
Now, my problem here is that an actual click will generate a single navigation of type "LinkClicked" followed by a dozen type "Other" (redirects and ad loads I assume), which gets handled correctly by the code, but a back/forward action will generate all its requests as back/forward requests.
In other words, a click calls setUrlQuietly: once, but a back/forward calls it multiple times.
I am trying to use this method to determine if the user actually initiated the action (and I'd like to catch page redirects too). But if the method has no way of distinguishing between an actual "back" and a "load initiated as a result of a back", how can I make this assessment?
Without this, I am completely stumped as to how I can only show the actual url and not intermediate urls. Thank you!
Ok here's what I ended up doing - not sure if this is the expected solution though. Scratch all the code from the above method and instead do this:
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSString *urlString = [self.webView stringByEvaluatingJavaScriptFromString:#"document.URL"]
[self setUrlQuietly:[NSURL URLWithString:urlString]];
}
This means there's a slight delay between clicking a link etc and seeing the url show up in the textField, but it also guarantees that the textField always shows the document's actual title.
I'm not seeing the UIWebViewNavigationTypeBackForward event at all. And sometimes webViewDidStartLoad never gets called. Perhaps there have been some changes in iOS 5.
Anyway, here's my solution for keeping the title and URL up to date. Works very consistently, though the response could be a little faster.
- (void)webViewDidFinishLoad:(UIWebView *)wv
{
[self updateButtons];
titleLabel.text = [wv stringByEvaluatingJavaScriptFromString:#"document.title"];
NSURL *url = wv.request.URL;
[selectedUrl release];
selectedUrl = [url retain];
}

iPhone architect choice for large text flow w/links

I am designing an app which will present large amounts of text that is interspersed with notes and references as clickable images. On a PC I'd use a control that shows HTML, but in the iPhone I am not able to intercept the touches of images and links too well using the UIWeb control.
Should I use a UIScroll and build the text as lables and UIImages perhaps?
Looking for the best way forward in my design phase.
I don't know what your requirements are obviously, but it is possible to capture the click on an link in a UIWebView and take some alternative action. In one of my Apps, I have a UIWebView with one particular link which I want to route differently, while I let all other links open as web pages in the UIWebView as normal. Here's the code snippet from the app which accomplishes this. It is within a UIViewController which loads the UIWebView:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [ request URL ];
if( [[url path] isEqualToString:#"/the_special_link.html"] ) {
// Take some alternative action and then stop the page from loading...
// (code to take some special action goes here)
return NO;
}
else {
return YES;
}
}
This is a delegate method call, so when I set up the UIWebView, which I do programmatically in the Controller loadView method, I set the WebView's delegate to that same Controller:
myWebView.delegate = self;