SlopTonio
SlopTonio

Reputation: 1105

Check if valid url WebView Swift

I want to check if the video url works before embedding it into my webView. Because if the user enters a url that doesn't resolve or contain video then the webView will display a whitebox

  // (if MyVariables.link is valid)
  {....
  let Code:NSString = "<iframe width=\(width) height=\(height) src=\(MyVariables.link)  frameborder=\(frame)></iframe>";
  self.wView.loadHTMLString(Code as String, baseURL: nil)
  }

Upvotes: 4

Views: 2116

Answers (1)

Christian
Christian

Reputation: 22343

Check the url by using canOpenURL from UIApplication

 if let url = NSURL(string: yourUrlString) {
    var canOpen = UIApplication.sharedApplication().canOpenURL(url)
 }

Upvotes: 7

Related Questions