1
votes

That's my first question here after years as a web developer now trying to code an OSX App using Xcode and Swift (latest).

What I'm trying to do is to execute JavaScript on the Webpage I'm loading with Webkit/WebView. I tried so many ways (the internet is full of answers - mostly for iOS) but there is quite little for OSX + Swift + WebView (not UIWebview or WkWebView).

My latest approach is this ViewController.swift:

import Cocoa
import WebKit

class ViewController: NSViewController, WebFrameLoadDelegate {
    var myWebView:WebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        myWebView = WebView(frame: self.view.frame)
        myWebView.frameLoadDelegate = self

        //Load URL
        let url = NSURL(string: "http://google.com");
        let request = NSURLRequest(URL: url!);

        myWebView.mainFrame.loadRequest(request)
        self.view.addSubview(myWebView)

        //Insert JS
        myWebView.stringByEvaluatingJavaScriptFromString("alert('test');");

        let script = "document";
        if let returnedString = myWebView.stringByEvaluatingJavaScriptFromString(script) {
            print("the result is \(returnedString)");
        }

    }

   /*
    * Page ready 
    */

    func webViewDidFinishLoad(myWebView : WebView) {
        print("WvFinished")
    }

    func webViewDidStartLoad(myWebView : WebView) {
        print("WvStarted")
    }

}

Neither the JS-Alert nor the return of the html document works.

PS: Neither the "webViewDidFinishLoad" nor the "webViewDidStartLoad" seems to be executed.

I would really appreciate any help. I guess I've almost searched the whole web for all hints - but nothing worked for me.

Thank you in advance!

Best,

Alex

2
This is not iOS WebFrameLoadDelegate has no method webViewDidFinishLoad and/or webViewDidStartLoadLeo Dabus
If you would like to monitor your webpage loading you can use func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!) { print("didFinishLoadForFrame") }Leo Dabus
You are totaly right, thank you. I tried to use the "stringByEvaluatingJavaScriptFromString" method here, but with the same result: No errors, but no effect at all. Do you have an idea?AlexDoe
Hey Leo! I tried myWebView.windowScriptObject.evaluateWebScript("alert('test');");, but same as always: No JS-Alert at all. Did you mean to use it that way? Best!AlexDoe

2 Answers

0
votes

About myWebView.windowScriptObject.evaluateWebScript("alert('test'‌);");, you should also do this webView(_:runJavaScriptAlertPanelWithMessage:initiatedBy:), and then you can print the message, you will get the message of the alert. And if you want to pop up a alert box, you need to do it yourself.

0
votes

Swift 4 WebView is deprecated. Please use WebKit instead.

import WebKit
//---

let webConfiguration = WKWebViewConfiguration()
let preferences = WKPreferences()
preferences.javaScriptEnabled = true

webConfiguration.preferences = preferences

webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.translatesAutoresizingMaskIntoConstraints = false

self.webView.evaluateJavaScript("document.getElementById('name').value='\(self.nameTextField.text)';", completionHandler:{ [weak self] _, error in
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        if error != nil {
            // Do something
            return
        }
    }
})