Reputation: 21
Using Swift 3 and UIWebView how do I force the YouTube logo/watermark “Hyperlink” that is displayed in a YouTube video playing through a webpage IFrame to open in Safari?
I am using a UIWebView (can’t transition to WKWebView just yet.) to display a web page that plays a YouTube video in an IFrame.
Following the “YouTube Embedded Players and Player Parameters” instructions at https://developers.google.com/youtube/player_parameters?csw=1#modestbranding. I was able to remove the title and share button from the top of the video using showinfo=0. This only leaves the YouTube logo/watermark.
When you click on the YouTube logo/watermark it opens YouTube inside my app, which is not good for the user navigation experience.
If a user clicks the YouTube logo/watermark I need this link to open in Safari. I have tried using the code shown below without success. The code works on every other link in the UIWebView but not on the YouTube logo/watermark hyperlink embedded in the actual video.
// ViewController.swift
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
if let url = URL(string: "http://www.example.com") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
//Force all links to open in Safari.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let url = request.url, navigationType == UIWebViewNavigationType.linkClicked {
UIApplication.shared.openURL(url)
return false
}
return true
}}
I was able to achieve this in the android version of my app using the example at WebView link click open default browser.
I have spent a week trying to figure this out so as not to waste any ones time with a question that has already been answered. Any help in figuring this out would be appreciated. Thank you!
Upvotes: 2
Views: 1307
Reputation: 1
Try UIWebViewNavigationType.other
to catch youtube.com/watch url insted of linkClicked:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let url = request.url, navigationType == UIWebViewNavigationType.other {
// catch "youtube.com/watch" and let safari to open link
return false
}
return true
}}
Upvotes: 0
Reputation: 175
I'm looking for the exact same solution and I'm wondering if anyone can help on this matter
I tried it with the following code snippet
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.linkClicked {
guard let abURL = request.url else { return true }
let abRequest = URLRequest(url: abURL)
print(abURL)
print(abRequest)
if abURL.absoluteString.range(of: "youtube") != nil {
// Open links in Safari
if #available(iOS 10.0, *) {
UIApplication.shared.open(abURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(abURL)
}
} else {
// Open links in Webview
webView.loadRequest(abRequest)
}
return false
}
return true
}
On line 5/6 I print the url if a link is clicked and when I click on that Youtube Watermark inside the video (included as a iframe) I don't get a print of that url so my guess is that swift doesn't "know" that a link is clicked. All the other links are working and printing the url.
I also included an iframe with just a link to youtube in it
<a href="https://youtube.com">Youtube Link</a>
and this link works and I can print the url and it opens in safari so the problem must be with the link inside the youtube embed container
Upvotes: 0