Seems... here we faced an issue.., When a user clicks Button on WkWebview which is not Caught by...
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
// -1 "other" seen when assigning the url programatically
// 0 "linkActivated" a link with an href attribute was tapped
// 3 "reload" page was refreshed
guard let urlAsString = navigationAction.request.url?.absoluteString else {
return
}
if navigationAction.navigationType.rawValue == 0 {
//***Download Logic here...
}
}
But it worked fine previously.
I try to catch the button by its ClassName via injecting js, returns a nil value. Because the WebView loadRequest response rendering in <iFrame>
Related
I am trying to prevent users from being able to navigate to different pages on a website by using WKNavigationDelegate and implementing the function below:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if let urlStr = navigationAction.request.url?.absoluteString {
if urlStr == "https://random.ca/#/profile" {
decisionHandler(.cancel)
return
}
}
decisionHandler(.allow)
}
This function seems to only be called when I first load the home page of the website and when I navigate to other pages inside of this site it is not triggered and so I cannot prevent the user from navigating to those pages.
Any advice would be greatly appreciated.
I found something
Just before calling webView.load()
add this
webView.isUserInteractionEnabled = false
check this Disable links in WKWebView? for more details
I am trying to catch a button click in my web view. Button "action" if I can say so, triggers certain url... I am not really able to catch this moment through WKNavigationDelegate. webview is set like this:
lazy var advertWebKitView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
webConfiguration.preferences = preferences
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
I am trying to catch that button click like this:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
guard let urlAsString = navigationAction.request.url?.absoluteString.lowercased() else {
return
}
if urlAsString.range(of: "...") != nil {
}
}
but this method doesn't trigger when button is clicked.
On android, it works and its done like this:
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
if (url.equals("...")) {
finish();
}
//...
super.doUpdateVisitedHistory(view, url, isReload);
}
what would be equal method and a way to track navigation correctly through wkwebview?
EDIT:
if there is some info needed about the webpage structure or so, please ask for details and I will add that too. I just didn't know which info may be relevant or helpful.
The button html looks like this:
<a class="lc-button lc-button--light" href="/close">OK</a>
Also one interesting thing is, that I have tried to use my code on other sites and navigation delegate methods were triggered correctly. Which leads me that problem is with our site and its html code or something about how the page is loaded after button is clicked, maybe...
I am using this myself and it works, it is slightly different than what you are trying until now but the right equivalent to the Android method.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if let requestURL = navigationAction.request.url, requestURL.pathComponents.contains("...") {
//Do your things
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
Use RedirectTo instead of LinkTo in react router for redirecting to /close
First set delegate in viewDidLoad,
self.webView.navigationDelegate = self
You can catch where your button is navigating you to by implementing
WKNavigationDelegate function, didFinish navigation
extension YourViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let currentURL = webView.url{
if self.urlString == currentURL.absoluteString{
print(self.urlString)
}
}
}
}
I have a viewController showing a webkit view.
If the user browses to a certain type of link, then I want to move from that viewController to a new one, which can parse that specific type of url and display results (rather than moving to a new page)
However, as I understand it, in order to check the url links to see it's of the right kind then I need to use the function
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
but this only gives me the ability to write a bit of code to look at the URL link, and return a decision on whether to proceed to it or not, rather than what I want to do which is look at the URL link and then effectively segue to a completely new viewController to parse it.
Any ideas on how I should construct please?
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void)
{
if(navigationAction.navigationType == .other)
{
if navigationAction.request.url != nil
{
// Put your condition check on url.
// Create your view controller instance from storyboard. Pass the url to it and navigate.
}
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
I have a WKWebView where a user goes through several pages to reach a page which contains a video from lesson.ly.
The problem is that the video will not show up at all within the webview, there is just a blank gap in the page where the video would be. However, if I open chrome on iOS, it loads just fine.
I've pinpointed the issue by debugging the HTML code. Here is a screen shot of the HTML on the webview:
And Here's a picture of the HTML on Chrome for Mac:
If you noticed, there is nothing within the iframe for the webview. I have no idea why it's not loading the data. I have enabled 'Allows Arbitrary Loads' for App Transport Security to no avail.
Any help appreciated, thanks.
Alternatively, instead of allowing specific URLs, you can check if request in question is targeting main frame or not:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void) {
// Allow navigation for requests loading external web content resources.
guard navigationAction.targetFrame?.isMainFrame != false else {
decisionHandler(.allow)
return
}
...
}
Figured out the problem. It was all in the delegate method webView:decidePolicyForNavigationAction:decisionHandler: and because the Lessonly videos come from a different source (fast.wistia.net) I had to add that url expliclity.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
guard let requestURL = navigationAction.request.url?.absoluteString else { return }
if requestURL.tc_contains("mydomain.lesson.ly") || requestURL.tc_contains("fast.wistia.net"){
decisionHandler(.allow)
}
else {
decisionHandler(.cancel)
}
}
I'm trying to include an online form in my application using a UIWebView, and I noticed that once the user finishes the form, s/he can navigate to different addresses. Is there any way I can restrict the domain/url access of a webkit?
(This used to be possible using UIWebView example: https://stackoverflow.com/questions/7673116/restrict-uiwebview-to-certain-pages‌ but it's now deprecated)
WKWebView can utilize the WKNavigationDelegate to restrict navigation.
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.URL {
if url == permittedUrl {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
}
}