Skip to content

How I can return result of js or any function to the screen of iPhone? by ContentView

    //  ContentView.swift
//  Shared

import Foundation
import SwiftUI
import JavaScriptCore

class cube {
    var result: String
    
    func do_js(text: String) -> String {
        
let jsSource = "var testFunct = function(message) { return "Test Message: " + message;}"

var context = JSContext()
context?.evaluateScript(jsSource)

let testFunction = context?.objectForKeyedSubscript("testFunct")
        var result = testFunction?.call(withArguments: [text]).toString()
    return result!
    }
}


struct ContentView: View {
    
    cube().do_js(text: "Hello world") // Starts forom here
    
    var show_text = lol().result
    
    var body: some View {
        Text(show_text)
            .font(.body)
            .fontWeight(.black)
            .foregroundColor(Color.red)
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

(Sorry, I’m beginner and come to swift even not from js, but from python! So it’s incredibly new for me. But js more understandable for me from python.)

enter image description here

Answer

Here’s the simplest version. In this version, it basically uses your original pattern where doJS returns a value. The disadvantage to this version is that doJS will get called every time the view renders.

class Cube {
    func doJS(text: String) -> String? {
        let jsSource = "var testFunct = function(message) { return "Test Message: " + message;}"

        let context = JSContext()
        context?.evaluateScript(jsSource)

        let testFunction = context?.objectForKeyedSubscript("testFunct")
        return testFunction?.call(withArguments: [text]).toString()
    }
}

struct ContentView: View {
    var body: some View {
        Text(Cube().doJS(text: "Hello, world!") ?? "No result")
            .font(.body)
            .fontWeight(.black)
            .foregroundColor(Color.red)
            .padding()
    }
}

And here’s a slightly different version. In this version, Cube is an ObservableObject with a @Published value that stores the result. It will only get called once in onAppear.

class Cube : ObservableObject {
    @Published var result : String?
    
    func doJS(text: String) {
        let jsSource = "var testFunct = function(message) { return "Test Message: " + message;}"

        let context = JSContext()
        context?.evaluateScript(jsSource)

        let testFunction = context?.objectForKeyedSubscript("testFunct")
        result = testFunction?.call(withArguments: [text]).toString()
    }
}

struct ContentView: View {
    @StateObject var cube = Cube()
    
    var body: some View {
        Text(cube.result ?? "No result")
            .font(.body)
            .fontWeight(.black)
            .foregroundColor(Color.red)
            .padding()
            .onAppear {
                cube.doJS(text: "Hello, world!")
            }
    }
}