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;
}
}
Related
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;
}
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
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
}
So I've created an app that would open an HTML page with some text and links on it. But if I click on a link I the page that would open after will not scale (obviously).
I know that I can scale the WebView in my first ViewController but in that case it will be hard to read my initial HTML page.
I've tried sevral methods:
scale my webView on link clicked:
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL!)
myWebView.frame = UIScreen.mainScreen().bounds
myWebView.center = self.view.center
myWebView.scalesPageToFit = true
}
return true
Or like that:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .LinkClicked:
// Open links in Safari
UIApplication.sharedApplication().openURL(request.URL!)
myWebView.scalesPageToFit = true
return false
default:
// Handle other navigation types...
return true
}
}
But to no succsess.
After that I've tried to set up a segue to my second ViewController in case link is clicked but the result was still the same.
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
let about = self.storyboard?.instantiateViewControllerWithIdentifier("openlink") as! dossierLink
self.navigationController?.pushViewController(about, animated: true)
}
return true
}
Can someone help me out on that one? Thank you!
My recommendtion would be to only load the content of next html page or url
webView.loadDataWithBaseURL(null,urlcontent, "UTF-8", null)
There are many ways to convert url to string content like Android Read contents of a URL (content missing after in result)