2017-12-31 123 views
1

Ich habe Code für die Arbeit Links und TextView könnte bearbeitet werden.Fehler beim Klicken auf eine Telefonnummer oder E-Mail-Adresse in TextView

Und die Links in der Anwendung geöffnet.

Mit Links funktioniert, aber mit (Mail-Adressen, Telefonnummern) funktioniert nicht

Wie kann ich dieses Problem beheben?


Fehler beim Klick auf eine Telefonnummer oder E-Mail-Adresse:

'NSInvalidArgumentException' Grund: ‚Die angegebene URL ein nicht unterstütztes Schema hat. Nur HTTP- und HTTPS-URLs werden unterstützt.


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 
} 

Antwort

2

Der Fehler, den Sie zu sehen sind, weil Sie eine E-Mail oder Telefonnummer in Safari zu öffnen versuchen, und es diese Art der Regelung nicht umgehen kann.

Ich nehme an, Sie möchten Links in Ihrem Safari-View-Controller öffnen und E-Mails und Telefonnummern mit etwas anderem öffnen.

Erster Wechsel zurück alle Links zu Handhabung und dann etwas tun, wie folgt aus:

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

Danke soviel) – B2Fq

Verwandte Themen