Disable pop-up window - WKWebView SwiftUI - swift

It is possible to show websites with the help of UIKit and a WKWebView in SwiftUI:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "http://bla.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
I know that you can disable cookies by writing:
myRequest.httpShouldHandleCookies = false
But is there a way to avoid showing the pop up window on websites and reject all cookies that are not needed?

Related

How to add back button History in Xcode using swift language

First of all I'm new in swift. I have a WebView app for iOS device using swift language , can add button back to previous page ?
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com/")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
let statusBarFrame = UIApplication.shared.statusBarFrame
let statusBarView = UIView(frame: statusBarFrame)
self.view.addSubview(statusBarView)
statusBarView.backgroundColor = .systemBackground
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}

Loader image before webView finish loading Swift 5

I have simple webview.
All I need is the image to disappear after webView finish loading.
This function work, but I get error on line "imageView.isHidden = false"
error is : " Use of unresolved identifier 'imageView' "
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
imageView.isHidden = false
}
Full code:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
public var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}
public var screenHeight: CGFloat {
return UIScreen.main.bounds.height
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let imageName = "icon.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height:screenHeight)
view.addSubview(imageView)
let myURL = URL(string:"https://google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
imageView.isHidden = false
}
}
You need to assign it to an instance property (like you already did for var webView: WKWebView!) and assign it - then you have access to it at a later point in time.
self.imageView = UIImageView(image: image!)
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
var imageView: UIImageView!
public var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}
public var screenHeight: CGFloat {
return UIScreen.main.bounds.height
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let imageName = "icon.png"
let image = UIImage(named: imageName)
imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height:screenHeight)
view.addSubview(imageView)
let myURL = URL(string:"https://google.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
imageView.isHidden = false
}
}
try this

Wkwebview overlying and hiding the toolbar

I am using wkwebview to show a webpage but it is overlaying the toolbar. If I remove
view = webView Then the toolbar can be seen but not the webpage. Been going around in circles with this and tried several possible solutions in other threads but nothing is working. Here is my code...
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate{
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string:"website address")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
}
}

uiDelegate method create webview with configuration not called

I have assigned delegate to wkwebview uiDelegate and implemented it's method create web view with configuration, but it is not being called. I have even placed breakpoint but it is not hitting.
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
webView.uiDelegate = self
let requestURL = URL(string: "https://www.google.com")!
let request = URLRequest(url: requestURL)
webView.load(request as URLRequest)
}
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let config = WKWebViewConfiguration()
return WKWebView(frame: webView.frame, configuration: config)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
}
It seems like it doesnt call the delegate on the initial init of the webview ... I recommend you to just create the webview from code
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
configuration.allowsAirPlayForMediaPlayback = true
configuration.mediaTypesRequiringUserActionForPlayback = []
webView = WKWebView(frame: containerView.frame, configuration: configuration)
webView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(webView)
NSLayoutConstraint.activate([
webView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
webView.topAnchor.constraint(equalTo: containerView.topAnchor),
webView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
the containerview is just a basic UIView set in my xib so just set this uiview in your xib instead of a wkwebview and you should be good to go

how to add navigation controller to WebView?

I have used the following code to create a webView app in swift. but I need to also add a navigation bar at the bottom that goes to different pages of the website. And those are just another link but the webView is taking the whole screen and I can't seem to add the Nav bar to it. Help is appreciated in advance.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBAction func goBack(_ sender: Any) {
self.webView.goBack()
}
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://test.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
In Xib or storyboard add a subview to the bottom And change web view bottom constraint constant value.
WebView constraint
SubView constraint