2015-10-08 143 views
4

我有一個UIViewControllerUITextView自動檢測其文本中的超鏈接。它工作正常,但我想用SFSafariViewController打開鏈接,因此我保留在我的應用程序「內部」,而不是打開一個單獨的瀏覽器,這是「開箱即用」的行爲。打開網站與SFSafariViewController從UITextView自動檢測鏈接

我已經採取了下面的步驟大綱,但網站仍然打開一個單獨的Safari瀏覽器,而不是我的應用程序。我沒有收到任何錯誤或警告,但檢測到的網站仍然在單獨的瀏覽器中打開,而不是在我的應用程序中打開。 UITextViewDelegate方法似乎沒有被調用(我扔了一個日誌語句來檢查)。

我看着UITextViewDelegate,我想我想用這個方法來打開年代由UITextView檢測網站:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
    // Fill code in here to use SFSafariViewController 
    return YES; 
} 

什麼我迄今所做的:

1)進口SafariServices ,做了委託聲明,並在MyViewController中聲明瞭一個委託屬性。 ^h

@import SafariServices; 

@interface MyViewController : UIViewController <UITextViewDelegate, SFSafariViewControllerDelegate> 

@property(nonatomic, weak, nullable) id< SFSafariViewControllerDelegate, SFSafariViewControllerDelegate > delegate; 

2)增加了一個代表部分,以我的.m文件,並試圖以這種方法來填補從上面的存根:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
    SFSafariViewController *websiteToOpen = [[SFSafariViewController alloc]initWithURL:URL entersReaderIfAvailable:YES]; 
    websiteToOpen.delegate = self; 
    [self presentViewController:websiteToOpen animated:YES completion:nil]; 
    return YES; 
} 

我敢肯定這是100%,我這個碴但我無法超越終點線。我錯過了什麼?

回答

1

我試圖做同樣的事情,但有不同的問題。

首先,您需要設置您的文本視圖的委託,以便觸發delegate方法。我在viewDidLoad中做到這一點:

myTextView.delegate = self; 

然後,告訴你UITextView與URL交互:

- (BOOL)textView:(UITextView *)textView 
     shouldInteractWithURL:(NSURL *)url 
     inRange:(NSRange)characterRange 
{ 
    [self presentViewController:websiteToOpen animated:YES completion:nil]; 
    return NO; 
} 

有了這個,我可以提出的Safari瀏覽器視圖控制器,雖然是空的,即沒有內容和沒有完成按鈕。所以我在做別的事情錯了......

+0

你可以編輯你的答案,並把它放在斯威夫特?提前致謝!! –

3

你在你的實現,它告訴系統,繼續前進,做你打算用這個URL做(在這種情況下結束返回YES,開蘋果瀏覽器)。如果您決定自己處理此問題,請改爲返回NO

如此說來,一些注意事項:

  • Safari瀏覽器視圖控制器僅支持HTTP/HTTPS的網址。在使用Safari視圖控制器之前,請確保您檢查了URL.scheme
  • 雖然textView:shouldInteractWithURL:inRange:文檔建議,你應該使用這個方法來攔截的URL上的水龍頭......

    您可以使用此方法來替代作用,比如在URL顯示網頁內容在當前應用程序的Web視圖中。

    ...你要小心。 「應該交互」不僅僅包括一個普通的點擊 - 這種方法也會被稱爲長按等等,這通常會有不同的行爲,並且我不知道如何確定爲什麼這個委託方法正在調用。

出於這些原因,你可能要考慮安裝自己UITapGesturRecognizerUITextView,如this answer詳述。

5

萬一有人在尋找斯威夫特3執行。

解決方案,工程:

1.Importing Safari瀏覽器服務

import SafariServices 

2.創建屬性文本,添加鏈接到它,指定字符串的TextView

let myLink = "google.com" 
let myText = "here is my link: \(myLink)" 
let myAttributedText = NSMutableAttributedString(string: myText) 
let rangeOfMyLink = myText.range(of: myLink) 
if rangeOfMyLink.location != NSNotFound { 
    myAttributedText.addAttributes([NSLinkAttributeName: myLink], range: rangeOfMyLink) 
} 
myTextView.attributedText = myAttributedText 
myTextView.delegate = self 

3.添加代表到VC:

class MyViewController: UIViewController, UITextViewDelegate 

4.Adding委託方法:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { 
    let safariVC = SFSafariViewController(url: URL) 
    present(safariVC, animated: true, completion: nil) 
    return false 
} 

希望它能幫助!