Hello I am having issues where when I open a link to YouTube in a wkwebview it opens up the installed YouTube app. I have tried my own way of blocking the opening of the YouTube app but it seems I'm stuck.
Is there a way to prevent certain apps from opening automatically from links clicked on or traveled to?
Here is what I have so far.
guard let url = navigationAction.request.url else {
decisionHandler(.cancel)
return
}
//not sure why this code is not being used...
// Check if the navigation is a link click with target="_blank"
if navigationAction.targetFrame == nil {
// Handle the navigation request to open a new window
if let url = navigationAction.request.url {
// UIApplication.shared.open(url) //opens in safari....
let request = URLRequest(url: url)
webView.load(request)
decisionHandler(.cancel)
return
}
}
// Continue with the navigation if not a link with target="_blank"
if url.scheme == "youtube" || url.scheme == "music" {
decisionHandler(.cancel)
return
}
//this should cancel app opening for youtube:// links and apple music.
// if let url = navigationAction.request.url {
// // Allow Google sign-in redirects
// if url.absoluteString.contains("accounts.google.com") || url.absoluteString.contains("gstatic.com") {
// decisionHandler(.allow)
// return
// }
// }
if url.absoluteString.hasPrefix("http") {
decisionHandler(.allow)
return
}
if let url = navigationAction.request.url {
if shouldDownloadFile(from:url) {
// if navigationAction.navigationType == .other, let mimeType = navigationAction.request.allHTTPHeaderFields?["Content-Type"], mimeType.contains("application/pdf") {
downloadFile (from: url)
decisionHandler(.cancel)
return
}
}
decisionHandler(.allow)
}