html - How to make webview only load specific website like youtube? -


i new swift , i'm making app webview can display youtube videos. problem don't want user go url or that. want make app load youtube.com. i'm using html instead of url because can tell webview height , width it's going have.

html code:

<html><head><style type=\"text/css\">body {background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><iframe frameborder=\"0\" height=\"" + string(describing: height) + "\" width=\"" + string(describing: width) + "\" src=\"http://www.youtube.com/embed/" + vid.videoid + "?showinfo=0&modestbranding=1&frameborder=0&rel=0\"></iframe></body></html> 

you try using shouldstartloadwith delegate method of uiwebviewdelegate

extension uiviewcontroller: uiwebviewdelegate {      public func webview(_ webview: uiwebview, shouldstartloadwith request: urlrequest, navigationtype: uiwebviewnavigationtype) -> bool {         let url = request.url?.absolutestring ?? ""          print("webview shouldstartloadwithrequest url -> \(url)")          if !url.contains("youtube.com") {              // avoid loading strange urls..             // manage error need..             webview.stoploading()              return false         }         return true     } } 

remember set uiviewcontroller uiwebview delegate in viewdidload method:

override func viewdidload() {     super.viewdidload()      mywebview.delegate = self } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -