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!
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
Youtube Link
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
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
}}
Related
I developed simple web browser with WKWebView in Swift.
When I click Youtube link, Youtube app is auto launched.
I hope to play Youtube video inside my web browser.
I don't need to launch Youtube app.
Please help me.
Here are my sample code.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let webViewConfiguration = WKWebViewConfiguration()
webViewConfiguration.allowsInlineMediaPlayback = true
wkWebView.configuration.allowsInlineMediaPlayback = true
wkWebView.navigationDelegate = self
let myURL = URL(string: "https://www.google.com")
let youtubeRequest = URLRequest(url: myURL!)
wkWebView.load(youtubeRequest)
}
Yes, you can do this.
First you need to set up a WKNavigationDelegate in your webview, then in webview:decidePolicyFor method cancel any link activated youtube requests, and force it to load within your webview.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated && navigationAction.request.url?.host?.hasSuffix("youtube.com") {
decisionHandler(.cancel)
DispatchQueue.main.async {
webView.load(navigationAction.request)
}
} else {
decisionHandler(.allow)
}
}
Unfortunately you can't do that programatically. Following are the steps to achieve something like you want manually.
Open youtube app from Home screen
Tap on your profile picture on the upper right corner.
Go to Settings and tap on Google app Settings
Check the Safari Option.
I am implementing a webview for loading contents from a URL in my app using web view. I want that url with target="_blank" should open in browser or else open in app only.
I am using
url = NSURL (string: "\(urlBase)\(response![0].Message ?? "")")!
let requestObj = URLRequest(url: url as URL)
webVFAI.loadRequest(requestObj)
this opens url in app but I want if my url contains target ="_blank", it should open it in browser. How to handle this?
Please guide
I just found the solution. I just checked for my pageid to decide whether to open url in browser or in app
I used shouldStartLoadWith function
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if(condition) {
switch navigationType {
case .linkClicked:
guard let url = request.url else { return true }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
return false // opens in browser
default:
return true // opens in app
}
}
return true;
}
When I browse some link in my app(in UIWebView), it opens the that link's app installed in my device. How can I restrict it to open external app and load the same URL in my UIWebView.
Maybe someone will find it useful:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked, let req = request.urlRequest {
webView.loadRequest(req)
return false
}
return true
}
Thus, I block the opening of the link in the side application, such as YouTube app, but open it in the UIWebView.
You can use func webView(UIWebView, shouldStartLoadWith: URLRequest, navigationType: UIWebViewNavigationType) in UIWebViewDelegate to do that. For example:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let urlString = request.url?.absoluteString ?? ""
if urlString == <your app link on webview> {
return false
}
return true
}
You now just replace <your app link on webview> with your actual link that you don't want web view to navigate to
I have one url request let say
let url = URL(string: "https://www.google.com/share?referCode=RSJDofpeW")
and we want to open this url in WebView and this link is also associated with the universal links. So opening this url will open the installed application but i want to load the page in UIWebView. So i checked the delegates and found that at first time it calls it is the above url then next time it will add scheme and appstore redirection.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
log.verbose(webView)
log.verbose(request)
log.error(navigationType.rawValue)
if request.url?.scheme == "itms-appss" || request.url?.scheme == "googleApp"{
return false
}
return true
}
So to overcome the issue of not loading the page in Web View i did the code like above so when scheme is itms-appss or googleApp it will not load the request but for the first time when it was correct url it should load that page but that is not opened.
//Check
var isUrlLoaded : Bool = false;
//delegate functions
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//check is the url is loaded for first time or not
if isUrlLoaded != true{
if request.url?.scheme == "itms-appss" || request.url?.scheme == "googleApp"{
return true
}
}
return false
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if webView.request?.url?.scheme == "itms-appss" || webView.request?.url?.scheme == "googleApp"{
// is url loaded
isUrlLoaded = true;
}
}
I have UITextField and UIWebView like this:
enter image description here
But when I clicked a link inside webview, the textfield above did not change to clicked URL.
I try with :
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let scheme = request.URL!
print(scheme)
}
and this:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .LinkClicked {
print(request.URL.path)
}
return true
}
and this:
textfield.text = webview.request.URL.absoluteString
but it did not get request.URL.path and webview.request.URL.absoluteString.
How to make texfield.text change to clicked URL from webview?
If you are targeting iOS 8 and later, use WKWebView instead of UIWebView. From Apple:
In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView. Additionally, consider setting the WKPreferences property javaScriptEnabled to false if you render files that are not supposed to run JavaScript.
The minor annoyance is that WKWebView can be dragged and dropped via Interface Builder so you have to add it to your view in code. Here's the navigation part:
override func viewDidLoad() {
super.viewDidLoad()
self.webView.navigationDelegate = self
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
self.addressField.text = webView.URL!.absoluteString
}