I'm developing and iPhone 3.0 application. And I'm trying to open web links in a UITextView into a UIWebView instead of Safari. But still no luck.
The UITextView
is not editable, and it perfectly detects web links and open them in Safari.
How to avoid that? How to grab that url so i can use with my own UIWebView
?
The simplest way is to override the
webView:decidePolicyForNavigationAction:request:frame:decisionListener:
method onUITextView
like so:This will affect all
UITextView
s in your application. If you only require this on a single view, create a subclass and override the method on that.Note: this is technically a private API and could be removed at any time. There is no way to do this via the public API.
edit: as of iOS 7.0 a new method has been introduced on
UITextViewDelegate
to support this. See nihad's answer for details.With Swift 3,
UITextViewDelegate
provides atextView(_:shouldInteractWith:in:interaction:)
method.textView(_:shouldInteractWith:in:interaction:)
has the following declaration:The following code shows how to open
UITextView
web links in aSFSafariViewController
instead of opening them in Safari app:This is an old question but incase anyone is looking for an updated way of doing this.
Assign your viewController that holds the UITextView as a delegate in your viewController's .m file and just add: