2009-08-20 84 views
15

我正在開發和iPhone 3.0應用程序。我試圖在UITextView中打開一個UIWebView而不是Safari的網頁鏈接。但仍然沒有運氣。如何在UIWebView而不是Safari中打開UITextView網頁鏈接?

UITextView不可編輯,它完美檢測到網頁鏈接並在Safari中打開它們。

如何避免這種情況?如何抓取該網址,以便我可以使用我自己的UIWebView

回答

7

最簡單的方法是重寫webView:decidePolicyForNavigationAction:request:frame:decisionListener:方法上UITextView像這樣:

@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 

這將影響到所有UITextView在你的應用程序。如果您只需要在單個視圖上進行此操作,請創建一個子類並覆蓋該方法。

注意:這在技術上是一個私人API,可隨時刪除。無法通過公共API執行此操作。

編輯:從iOS 7.0開始,在UITextViewDelegate上引入了一種新方法來支持這一點。有關詳細信息,請參閱nihad的答案。

+1

嗯,是私人的,你認爲這會是appstore批准的問題? Btw今晚我會檢查你的代碼,我會讓你知道。謝謝。 – crash 2009-08-20 17:49:45

+0

這不是他們可以檢查的東西,但是您可能會對未來的操作系統更改造成輕微的風險(這是桌面應用程序中的公共API,Apple不太可能停止使用WebKit,但這是可能的) 。可能發生的最壞情況是行爲將返回默認啓動MobileSafari。 – rpetrich 2009-08-20 17:59:02

+0

完美。它工作順利。謝謝! – crash 2009-08-20 22:24:08

20

這是一個古老的問題,但任何人都在尋找更新的方式來做到這一點。

指定您的viewController是持有的UITextView在你的viewController的.m文件的委託,只是補充:

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{ 

    //Do something with the URL 
    NSLog(@"%@", URL); 


    return NO; 
} 
+0

我相信這是iOS 7的正確方式。這對我有幫助,所以謝謝! – ajfigueroa 2014-02-10 07:12:50

+0

@nihad非常感謝,這比以前的解決方案看起來更清潔和簡單。 – sunny 2015-06-23 14:54:54

2

與SWIFT 3,UITextViewDelegate提供了textView(_:shouldInteractWith:in:interaction:)方法。 textView(_:shouldInteractWith:in:interaction:)具有以下聲明:

詢問委託如果指定的文本視圖應該允許用戶交互的與文本的給定範圍內的給定的URL指定的類型。

optional func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool 

下面的代碼演示瞭如何在SFSafariViewController打開UITextView網絡鏈接,而不是在Safari瀏覽器的應用中打開其中:

import UIKit 
import SafariServices 

class ViewController: UIViewController, UITextViewDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Set textView 
     let textView = UITextView() 
     textView.text = "http://www.yahoo.fr http://www.google.fr" 
     textView.isUserInteractionEnabled = true 
     textView.isEditable = false 
     textView.isSelectable = true 
     textView.dataDetectorTypes = UIDataDetectorTypes.link 

     // Add view controller as the textView's delegate 
     textView.delegate = self 

     // auto layout 
     view.addSubview(textView) 
     textView.translatesAutoresizingMaskIntoConstraints = false 
     textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
     textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 
     textView.heightAnchor.constraint(equalToConstant: 300).isActive = true 
     textView.widthAnchor.constraint(equalToConstant: 300).isActive = true 
    } 

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { 
     // Open links with a SFSafariViewController instance and return false to prevent the system to open Safari app 
     let safariViewController = SFSafariViewController(url: URL) 
     present(safariViewController, animated: true, completion: nil) 

     return false 
    } 

} 
相關問題