如何在 UIWebView 而不是 Safari 中打开 UITextView Web 链接?
我正在开发 iPhone 3.0 应用程序。我正在尝试将 UITextView 中的 Web 链接打开到 UIWebView 而不是 Safari 中。但仍然没有运气。
UITextView
不可编辑,它可以完美检测网页链接并在 Safari 中打开它们。
如何避免这种情况?如何获取该 url,以便我可以将其与我自己的 UIWebView 一起使用?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个老问题,但万一有人正在寻找更新的方法来做到这一点。
在 viewController 的 .m 文件中分配将 UITextView 保存为委托的 viewController,然后添加:
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:
在 Swift 3 中,
UITextViewDelegate
提供了textView( _:shouldInteractWith:in:interaction:)
方法。textView(_:shouldInteractWith:in:interaction:)
具有以下声明:以下代码演示如何在
SFSafariViewController
中打开UITextView
Web 链接,而不是在 Safari 应用程序中打开它们: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:最简单的方法是覆盖
webView:decidePolicyForNavigationAction:request:frame:decisionListener:
方法UITextView
类似so:这将影响应用程序中的所有
UITextView
。如果您只需要在单个视图上执行此操作,请创建一个子类并重写该子类的方法。注意:从技术上讲,这是一个私有 API,可以随时删除。无法通过公共 API 来执行此操作。
编辑:从 iOS 7.0 开始,
UITextViewDelegate
上引入了一种新方法来支持此操作。详情请参阅 nihad 的回答。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.