Reputation: 550
I'm trying to catch the YouTube URL in UITextView with this delegate method to open it on Youtube app:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.absoluthePath.contains("youtube.com"), let youtubeURL = URL(string: "youtube://" + id) {
UIApplication.shared.open(youtubeURL)
}
}
And I have this error:
Cannot call value of non-function type 'URL'
Because Xcode thinks I'm using 'URL' variable instead of using URL(string: "")! declaration
Upvotes: 0
Views: 574
Reputation: 285290
There are 3 issues in your code, but first of all this is a pretty bad naming by Apple. The internal parameter label should be url
(lowercased). But as internal parameter labels are arbitrary, you can change the signature yourself to
func textView(_ textView: UITextView,
shouldInteractWith url: URL,
in characterRange: NSRange,
interaction: UITextItemInteraction) -> Bool {
The other issues are:
Bool
value.absoluthePath
in URL
, you mean absoluteString
A more efficient domain check is to use the host
parameter of the URL
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if url.host?.hasSuffix("youtube.com") == true, let youtubeURL = URL(string: "youtube://" + id) {
UIApplication.shared.open(youtubeURL)
return true
}
return false
}
Upvotes: 2
Reputation: 19044
You can use Foundation.URL
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if let youtubeURL = Foundation.URL(string: "youtube://" + id), URL.absoluteString.contains("youtube.com") {
UIApplication.shared.open(youtubeURL)
}
}
Upvotes: 1