Get URL of clicked link inside of UIWebView use Swift 2 - ios

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
}

Related

Disable UIWebView for opening links to redirect to apps installed on my iPhone

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

Swift 3 UIWebView open (YouTube logo/watermark hyperlink from video) in Safari

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
}}

disable a specific link on ios uiwebview

How can I disable a specific link on iOS uiwebview ?
The page loaded in the webview can have thousand urls but i want to disable one url.
I am sensing i have to use this method for the work. But can't figure it out.
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
}
I'm working with objective-c. Sorry if my swift code is wrong. I hope you understand with the logic.
Try it.
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let surl = request.url?.absoluteString
if surl == "http://disableurl.com" {
return false;
}
return true;
}

Call only first request UIWebView

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;
}
}

Scale web page on link clicked

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)

Resources