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;
}
Related
We'd like to load UIWebView (or WKWebView) content from a controlled list of URLs. We'd like to make sure there is no possible web navigation. We thought of webView:shouldStartLoadWith:navigationType: but the request.url from webView is appending a trailing slash to the URL's host, making it unfit for direct comparison.
Example of distinct URLs we'd like to load:
// example 1
let url = URL(string: "https://example.com/foo/")!
// example 2
let url = URL(string: "https://example.com/foo")!
// example 3
let url = URL(string: "https://example.com")!
How we load requests:
webView.loadRequest(URLRequest(url: url))
How we control requests:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return request.url == url
}
First example works, because request.url is indeed equal to https://example.com/foo/.
Second example works, because request.url is indeed equal to https://example.com/foo.
But third example fails, because request.url is now https://example.com/ instead of https://example.com
To support proper comparison, how to normalize an URL the same way as UIWebView does?
One workaround is you can take the absoluteString property of URL and delete the last element if it is '/' and compare.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let url = request.url{
var requestUrlArray = url.absoluteString.map { String($0) }
if let lastElement = requestUrlArray.last, lastElement == "/"{
_ = requestUrlArray.popLast()
}
let requestUrlString = requestUrlArray.joined()
return requestUrlString == url.absoluteString
}
return false
}
From your question, it seems that you want to allow urls with the same host, in which case…
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return request.url.host == url.host
}
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
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
}}
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 obtained the following code from here to open all other links that do not match my domain in Safari:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
return true
}
Although how can I allow another specified domain to be opened within my UIWebView instead of Safari, such as paypal.com?
You can store a list of allowed URLs and filter on the host name of the request URL. If the host matches one of the allowed URLs then return true to allow the URL to load in the web view. Otherwise use UIApplication.openURL() to open the URL in Safari.
For example:
let safeList = [ "paypal.com", "google.com" ]
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
if let host = request.URL?.host where safeList.contains(host) {
return true // Open in web view
}
UIApplication.sharedApplication().openURL(request.URL!)
return false
}
return true
}