2017-12-31 86 views
1

リンクを操作するためのコードがあり、TextViewを編集することができます。TextViewで電話番号または電子メールアドレスをクリックするとエラーが発生する

リンクがアプリケーションで開きます。リンク付き

は動作しますが、(メールアドレス、電話番号)で

私はこれをどのように修正することができますが動作しませんか?


エラーの電話番号やメールアドレスをクリックすると:

'NSInvalidArgumentException'、理由は:「指定されたURLはサポートされていない方式を採用しています。 HTTPとHTTPSのURLのみがサポートされています。


import SafariServices 

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 
} 
override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 
    NotificationCenter.default.removeObserver(self) 
} 
@objc func viewTapped(_ aRecognizer: UITapGestureRecognizer) { 
    self.view.endEditing(true) 
} 
// when you tap on your textView you set the property isEditable to true and you´ll be able to edit the text. If you click on a link you´ll browse to that link instead 
@objc func textViewTapped(_ aRecognizer: UITapGestureRecognizer) { 
    viewText.dataDetectorTypes = [] 
    viewText.isEditable = true 
    viewText.becomeFirstResponder() 
} 
// this delegate method restes the isEditable property when your done editing 
func textViewDidEndEditing(_ textView: UITextView) { 
    viewText.isEditable = false 
    //viewText.dataDetectorTypes = .all 
    viewText.dataDetectorTypes = .link 
} 

答えて

2

あなたはSafariで電子メールまたは電話番号を開こうとしていると、それはスキームのこのタイプを扱うことができないので、あなたが見ているエラーです。

Safariの表示コントローラーでリンクを開き、電子メールと電話番号を別のもので開きたいとします。

最初の変更バックすべてのリンクを処理すると、このような何かを:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { 
    if (URL.scheme?.contains("mailto"))! { 
     // Handle emails here 
    } else if (URL.scheme?.contains("tel"))! { 
     // Handle phone numbers here 
    } else if (URL.scheme?.contains("http"))! || (URL.scheme?.contains("https"))! { 
     // Handle links 
     let safariViewController = SFSafariViewController(url: URL) 
     present(safariViewController, animated: true, completion: nil) 
    } else { 
     // Handle anything else that has slipped through. 
    } 

    return false 
} 
+0

はどうもありがとうございます) – B2Fq

関連する問題