iOS Wkwebview Swift JavaScript Enable

WKWebView not working even after enabling Javascript - Swift

I've tested your new url and it's working perfectly fine. Try to use below Code:

import UIKit
import WebKit
class WebViewController: UIViewController {

let webView: WKWebView = {
let v = WKWebView()
return v
}()

lazy var backButton: UIButton = {
let v = UIButton()
v.layer.cornerRadius = 5
v.clipsToBounds = true
v.backgroundColor = UIColor.black.withAlphaComponent(0.5)
v.setTitle("<", for: .normal)
v.setTitleColor(.white, for: .normal)
return v
}()

var url: String?
var activityIndicator: UIActivityIndicatorView?

func setupUI(){
view.backgroundColor = .black
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator?.center = self.view.center
[webView,backButton,activityIndicator!].forEach{view.addSubview($0)}
webView.fillSuperView()
if #available(iOS 11.0, *) {
backButton.anchor(top: view.safeAreaLayoutGuide.topAnchor, left: view.leadingAnchor, bottom: nil, right: nil, size: .init(width: 30, height: 30), padding: .init(top: 8, left: 20, bottom: 0, right: 0))
} else {
backButton.anchor(top: topLayoutGuide.bottomAnchor, left: view.leadingAnchor, bottom: nil, right: nil, size: .init(width: 30, height: 30), padding: .init(top: 8, left: 20, bottom: 0, right: 0))
}
}

override func viewDidLoad() {
super.viewDidLoad()
setupUI()
backButton.addTarget(self, action: #selector(didSelect(_:)), for: .touchUpInside)

}

@objc func didSelect(_ sender: UIView){
switch sender {
case backButton:
navigationController?.popViewController(animated: true)
default:
break
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
webView.navigationDelegate = self
webView.load(URLRequest(url: URL(string: url ?? "https://demo.dukanje.com/index.php?route=product/product_option&product_id=206&store_id=52&language_id=1")!))
activityIndicator?.startAnimating()
}
}

extension WebViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.activityIndicator?.stopAnimating()
self.activityIndicator?.removeFromSuperview()
self.activityIndicator = nil
}
}

I've used extensions to help layout:

extension UIView{

func fillSuperView(){
translatesAutoresizingMaskIntoConstraints = false
guard let superview = superview else {return}
anchor(top: superview.topAnchor, left: superview.leadingAnchor, bottom: superview.bottomAnchor, right: superview.trailingAnchor)
}

func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, size: CGSize = .zero, padding: UIEdgeInsets = .zero){
translatesAutoresizingMaskIntoConstraints = false
if let top = top{
topAnchor.constraint(equalTo: top, constant: padding.top != 0 ? padding.top : 0).isActive = true
}
if let left = left{
leadingAnchor.constraint(equalTo: left, constant: padding.left != 0 ? padding.left : 0).isActive = true
}
if let bottom = bottom{
bottomAnchor.constraint(equalTo: bottom, constant: padding.bottom != 0 ? -padding.bottom : 0).isActive = true
}
if let right = right{
trailingAnchor.constraint(equalTo: right, constant: padding.right != 0 ? -padding.right : 0).isActive = true
}
if size.width != 0{
widthAnchor.constraint(equalToConstant: size.width).isActive = true
}
if size.height != 0{
heightAnchor.constraint(equalToConstant: size.height).isActive = true
}
}
}

First:
Sample Image
Second:
Sample Image

Enable Javascript in WKWebView iOS 9

JavaScript is enabled by default. There is a javaScriptEnabled property on WKPreferences that you can flip to true in case some code has turned it off.

Are you sure that the problem you are seeing is related to JavaScript? Do you see any errors in the console? Did you implement the error/failure delegate methods to catch problems?

My gut feeling is that you are running into something different. For example a resource not loading because of the new App Transport Security requirements in iOS9.

Swift WKWebView disable Javascript

WKWebView has a configuration to disable JavaScript, check the Apple reference.

var javaScriptEnabled: Bool

Update

let preferences = WKPreferences()
preferences.javaScriptEnabled = false

// Create a configuration for the preferences
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences

// Instantiate the web view
webView = WKWebView(frame: view.bounds, configuration: configuration)

// Load page
if let theWebView = webView{
let url = NSURL(string: "http://www.apple.com")
let urlRequest = NSURLRequest(URL: url!)
theWebView.loadRequest(urlRequest)
theWebView.navigationDelegate = self
view.addSubview(theWebView)
}


Related Topics



Leave a reply



Submit