Don't allow Webview to open hyperlinks correctly - ios

For example, I write as follows, then it works correctly, that is, the content that comes is displayed as it should and when the user clicks the link, it doesn't redirect anywhere.
extension AuthorizationContentView: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
}
BUT I need to cover all the cases of clicking on the link and not just linkActivated(A link with an href attribute was activated by the user), but if I write just decisionHandler(.cancel), then the content is not displayed and it is unclear why so.
extension AuthorizationContentView: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.cancel)
return
}
}
UPD:
func setupWebView(content: String) {
let supportDarkCSS = "<style>:root { color-scheme: light dark; }</style>"
contentStackView.removeAllArrangedSubviews()
webView.loadHTMLString(content + supportDarkCSS, baseURL: nil)
contentStackView.addArrangedSubview(webView)
}

Ok, so after I understand what you are trying to do, the .linkActivated navigation type does not include all of the cases you are trying to block.
I think the best options is to keep intercept the requests using the func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) delegate function.
You can use the URLRequest property on the WKNavigationActioni.e navigationAction.request.
Then you can use the URLRequest metadata, for example, the URL itself (navigationAction.request.url) or the HTTP method, and understand if that request needs to be blocked.

Related

SwiftUI WKWebView disable link interactions

I am looking for a way to disable a user interacting with links and images in SwiftUI using WKWebView. I have tried various ways but none have worked, and I have seen and tried the following post but nothing worked:
WKWebview allowsLinkPreview to false breaks text selection
WKWebview: Disable interaction and clicks on links
I am displaying a website that I have permission to display but due to its abnormal HTML structure it makes it hard to display the context, so I decided to try disabling user interactions with links. I do still want user interactions enabled so the user can scroll through the calendar presented to them, but not be able to click on links. If you have any suggestion I am open to them.
Here is my code:
struct CalanderWeb : UIViewRepresentable {
#State var request: URLRequest
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.scrollView.isScrollEnabled = true
uiView.isOpaque = false
uiView.allowsBackForwardNavigationGestures = false
uiView.load(request)
}
func makeCoordinator() -> CalanderWeb.Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate {
let parent: CalanderWeb
init(_ parent: CalanderWeb) {
self.parent = parent
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.cancel)
}
}
}
Thank You.
Here is correct delegate callback
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
depending of used HTML, probably also might be disabled .formSubmitted, if needed.
You'll have to override the method decidePolicyForNavigationAction in WKNavigationDelegate and provide .cancel in the completion block parameter decisionHandler. Here is the sample code:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.cancel)
}

I am not getting click event for one button of webpage in WKWebview , and how can I add the click event to wkwebview with using java script

I have posted the delegate method I used if there is no event for button action how can I add that event with javascript .how can I use the functionalists in my view controller.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
print(#function)
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
if url.absoluteString.contains("ios-development-course") {
// this means login successful
decisionHandler(.cancel)
}
else {
decisionHandler(.allow)
}
}
When you click a button which is basically a link in web view, the navigation happens in web view.
There is a delegate method you can use to allow or cancel that navigation.
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
var shouldLoad = true
if navigationAction.navigationType == .linkActivated.
//user clicked button on web view
decisionHandler(shouldLoad ? .allow : .cancel)
}
}

WKWeb View does finish loading of page in swift iOS?

I have created a WKWebView. It loads the url of MSB ie. My school bucks. It loads almost all url for payment. But at one url didFinishLoad is not getting called. I have added all the delegates for the web view.but certainly it is not working. The whole process works fine in the web browser of iPhone.
Below is the code I have added below code for that
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
self.webView = WKWebView(frame: frame, configuration: configuration)
self.view.addSubview(webView)
self.webView.navigationDelegate = self
self.webView.uiDelegate = self
Delegates Methods for the navigation
// MARK: - WKWebView Delegation
extension PaymentWebViewController:WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DILog.print(items: "didFinish")
self.hideLoader()
if let strUrl = webView.url?.absoluteString {
self.readRedirectUrl(stringUrl: strUrl)
}
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
DILog.print(items: "faluere")
self.hideLoader()
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
DILog.print(items: "didStatrt")
self.showLoader()
if let strUrl = webView.url?.absoluteString {
DILog.print(items: "URL IS \(strUrl)")
}
}
func webView(_ webView: WKWebView,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
{
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
{ DILog.print(items: "Auth Channlenge1")
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}
else
{ DILog.print(items: "Auth Channlenge2")
completionHandler(.performDefaultHandling, nil)
}
}
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
DILog.print(items: "webViewWebContentProcessDidTerminate")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
DILog.print(items: "didCommit")
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
DILog.print(items: "decidePolicyFor Action")
decisionHandler(WKNavigationActionPolicy.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void) {
DILog.print(items: "decidePolicyFor response ")
decisionHandler(.allow)
}
}
You need to add exception (SecTrustSetExceptions) for that particular url. As there is some conflict with its signing identity and your WKWebView.
I was also facing same issue in my app and successfully solved by using below snippet.
try this:
extension PaymentWebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void) {
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let serverTrust = challenge.protectionSpace.serverTrust
let exceptions = SecTrustCopyExceptions(serverTrust!)
SecTrustSetExceptions(serverTrust!, exceptions)
completionHandler(.useCredential, URLCredential(trust: serverTrust!))
}
}
Hope you've already setup this in your info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>

When does navigationAction.navigationType == .linkActivated?

In a WKWebView, every time I tap on a URL, the navigationType is .other. When does navigationType equal .linkActivated?
Very unlikely. Maybe you are interpreting the raw values wrong?
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.navigationType.rawValue)
// -1 "other" seen when assigning the url programatically
// 0 "linkActivated" a link with an href attribute was tapped
// 3 "reload" page was refreshed
}
See the docs where you can click on the enumerations listed and and see their raw values

How to monitor and block ajax requests in WKWebView?

From my understanding and trying out some code, we can only monitor page navigation requests using
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void)
{
if // some condition
{
decisionHandler(.allow) // to allow the page navigation.
}
else
{
decisionHandler(.cancel) // to cancel the page navigation.
}
}
But is there any way to monitor and block some ajax requests in WKWebView?
And also to capture the ajax response.

Resources