A class like UITextView, but with the ability to handle URL clicks - ios

I am wondering if there is something already like this out there. But I basically want something like UITextView to display text with embedded links. However, I want to be able to handle the URL clicks as a delegate.
I've read a few posts like the following:
How to intercept click on link in UITextView?
However, I really don't want to override the openURL method. My app works with lots of webServer data, and I don't want to keep creating exceptions for different hosts in the openURL method.
I guess my questions is, is there another way to intercept the click on UITextView?
My alternative is to write my own, with a UIScrollView, and use a TTTAttributedLabel (https://github.com/mattt/TTTAttributedLabel) inside it. But am looking for suggestions, or alternatives.
Thanks.

You may use UIWebView + Local HTML instead of UITextView.
And use the -(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType delegate to handle the URL clicks,like:
-(BOOL )webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType )navigationType
{
NSURL * clickedURL=[request URL];
//Do something here.
return NO;
}

Related

Remove html code from page URL

I need to display webpage using URL. From that page I want to remove one link control from that html page. Is it possible? I am opening it with this kind of code.
NSString *urlAddress = #"http://myurl.com";
NSURL *url = [NSURL URLWithString:urlAddress];
If you want to disable ALL links, see the answer from user3182143...
If you only want to disable a specific link, you can still use those methods - you will just need to evaluate the request part of shouldStartLoadWithRequest:(NSURLRequest *)request and decide whether to return YES or NO
If you want a specific link to look different, you'll need to modify the html. Couple ways to do so, but we really need more information about exactly what you are trying to do.
Are you displaying ONE specific web page? And all you need to do is change ONE link in that ONE page, and you're done? Are you displaying a series of pages? Do you have any control of the pages? And so on.
I'd suggest you start searching for and reading about uiwebview insert html or uiwebview insert javascript
We can do this.
First we should know about the UIWebViewDelegate methods
First create BOOL
ViewController.h
#property (nonatomic) BOOL ret;
ViewController.m
#synthesize ret;
Set BOOL to NO in below delagte method
- (void)webViewDidFinishLoad:(UIWebView*)webView {
// Sent after a web view finishes loading a frame.
ret = NO;
}
Then If we return NO in below delegate method, this will prevent the user from viewing link
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return ret:
}
Dave DeLong's Disable Link in UIWebView answer
Removing hyper links from a URL shown in UIWebView

Attributed Text View that opens address in new map view

I'm working on an app that uses parse as the backend and allows users to post their address via text view. I enabled the "Addresses" attribute and addresses open in the Apple maps app. Instead I'd like to keep my users in my app by opening a new view with a map and the address they tapped pinned to the map view.
Is there anyway I can do this without making things overly complicated?
Thanks for your help!
You can implement the UITextViewDelegate method:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange;
Description: Asks the delegate if the specified text view should allow
user interaction with the given URL in the given range of text.
The text view calls this method if the user taps or long-presses the
URL link. Implementation of this method is optional. By default, the
text view opens the application responsible for handling the URL type
and passes it the URL. You can use this method to trigger an
alternative action, such as displaying the web content at the URL in a
web view within the current application.
Reference
Ex:
Make sure your class conforms to: <UITextViewDelegate>
Set your textview delegate: [textView setDelegate:self];
Implement the delegate method:
...
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
// Open custom map using data from textView.text.
}

How to call a function after UIWebView shouldStartLoadingWithRequest method call is finished?

How to call a function after UIWebView shouldStartLoadingWithRequest method is finished executing in iOS?
I dont want to use performselector:afterdelay because that may not give accurate result while network is low.
Use webViewDidFinishLoad: this method is call after a web view finishes loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView
Parameters
webView:
The web view has finished loading.
Availability
Available in iOS 2.0 and later.
use these methods may be it helps you,
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//this method will call after successful loading
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//this method will call after getting any error while loading the web view.
}
The UIWebViewDelegate Protocol provides a few methods which can be used in your situation.
The first method that is called after shouldStartLoadWithRequest is webViewDidStartLoad: which, like its name suggests, is called once the WebView has started loading a frame.
From your question, this seems to be the most adequate method.
Other options, which were provided in previous answers, are webViewDidFinishLoad: and webView:didFailLoadWithError: which are respectively called every time the UIWebView finished loading a frame and whenever it fails to load a frame.
Note that this means that all methods described above may get called more than once before the UIWebView has actually finished loading, since they are thrown whenever the UIWebView is about to start, has just started, has finished or has failed loading each frame on a page.
Refer to the UIWebViewDelegate Protocol documentation for more detailed information.

UIWebView inside UICollectionViewCell does not call Delegate Method

I am currently embedding a UIWebView inside a UICollectionViewCell to be able to view HTML formatted text residing in my local data structure.
I know it has been stated many times one should not embed an UIWebView inside a scrollView, but i currently see no other way to achieve displaying mixed content overviews (That is the current requirement, and so far UICollectionView does work very well with large data sets).
The Problem is, that i am not able to call the UIWebViews Delegate method:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType { ... }
I need the delegate method to be able to display external links or call local methods.
Is there a way to achieve this? Maybe by handing over the events programatically?
Thanks for the feedback!
I just found out what i did wrong:
I just needed to update the cells' contentView: [[cell contentView] setFrame:frameContainingWebViewSize]
That did the trick. The delegate method was not called because the contentView did not cover the webview (clipSubviews is set to NO) and thus the tap events where not fired.

UIWebView won't goBack after loading HTML in iOS

I'm having this problem in my iPhone app: I have a webView which is first loaded with an HTML string. After the user clicks a link and it loads the requested page, the webView won't go back when I call the method [UIWebView goBack]; I suppose webView doesn't cache the HTML string. Is there any way I can make webView cache my HTML string without having to save it in a NSString myself?
You can use the canGoBack property and if you can't go back, reload the UIWebView with the original html. If the user has navigated forward using links then the canGoBack property will return YES and a goBack can be initiated on the UIWebView. The _htmlString is a member variable that is set when the UIWebView is initialized using an HTML string. -rrh
- (void)goBack
{
if (_htmlString && ![_browserWebView canGoBack]) {
[_browserWebView loadHTMLString:_htmlString baseURL:nil];
return;
}
[_browserWebView goBack];
}
Try to create a NSURLRequest from the file URL and use loadRequest instead of loadhtmlString
NSURL *htmlFileUrl = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:htmlFileUrl];
self.webView.delegate = self;
[self.webView loadRequest:localRequest];
This sounds like a your webview variable isn't properly linked up to the webview instance that you are using. Breakpoint at this call and check whether your webView variable is 'nil'.
If it is, make sure your webView in your XIB file is linked to an IBOutlet variable in Interface Builder. This is a common mistake and something I tend to forget when designing a new page for the first time.
This tutorial covers a LOT on how to build interfaces using Interface builder which i'm sure you're familliar with but for those that aren't it's also useful. It has some good screenshots which help illustrate what I mean by 'linking' better than me typing "click the little + icon and drag the little thingy on to the UI element" :)
http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/
EDIT
The only other thing i can think of is that you are overwriting your webView variable by re-initialising it somewhere (it is already initialised by the XIB) and therefore you're calling goBack on a webview that doesn't exist on the screen.

Resources