user2562126 user2562126 - 4 months ago 29
Swift Question

UITextViewDelegate function shouldinteractwithURL in Xcode 8.0

I have created a UITextView with detectable links however I am unable to use the UITextView delegate function 'shouldinteractwithURL'.

I get a warning that the instance method nearly matches another instance method. I have looked at the delegate functions and the 'shouldinteractwithURL' function has been updated for ios 10.0 however I want to use the version of the function that is compatible with ios 9. However, even if I use the new version and set the deployment target to ios 10, I get the same warnings and the function is still not called (I have sent the textview delegate correctly).

The solutions that Xcode suggests is to either make the function private or to add @nonobjc. Neither of these work so nothing happens when the links are clicked.

Has anyone else had this problem or is this an issue with Xcode 8.0?

Answer

With adding @availability attribute, this will compile without errors:

//For iOS 7...9
@available(iOS, deprecated: 10.0)
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange) -> Bool {
    var result = false
    //...
    return result
}

//For iOS 10
@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    var result = false
    //...
    return result
}
Comments