How to open UITextView web links in a UIWebView instead of Safari?

前端 未结 3 2014
孤独总比滥情好
孤独总比滥情好 2020-12-28 08:32

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 UIText

3条回答
  •  一向
    一向 (楼主)
    2020-12-28 09:11

    The simplest way is to override the webView:decidePolicyForNavigationAction:request:frame:decisionListener: method on UITextView like so:

    @interface UITextView (Override)
    @end
    
    @class WebView, WebFrame;
    @protocol WebPolicyDecisionListener;
    
    @implementation UITextView (Override)
    
    - (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
    {
        NSLog(@"request: %@", request);
    }
    @end
    

    This will affect all UITextViews 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.

提交回复
热议问题