直接从javascript调用swift类方法

时间:2018-12-13 04:31:03

标签: javascript ios swift wkwebview hybrid-mobile-app

嗨,我正在尝试实现javascript和ios之间的连接。通过以下代码,我成功使用messageHandlers从javascript创建了在ios中获取回调的连接

import UIKit
import WebKit

class ViewController: UIViewController, WKScriptMessageHandler {

    private var webView : WKWebView?

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        print("Message from beyond: \(message.body)")
        let jsMapper = JavaScriptInterfaceMapping()

        jsMapper.perform(Selector(("noParam")))
        jsMapper.perform(Selector(("oneParam:")), with: "firstParam")
        jsMapper.perform(Selector(("twoParams::")), with: [1, true], with: "Go Away")

        webView?.evaluateJavaScript("letSee()", completionHandler: nil)

    }

    override func loadView() {
        super.loadView()

        let  controller = WKUserContentController()
        controller.add(self, name: "xyz")

        let config = WKWebViewConfiguration()
        config.userContentController = controller

        self.webView = WKWebView(frame: self.view.frame, configuration: config)
        self.view = self.webView!

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = Bundle.main.url(forResource: "index", withExtension: "html")
        let request = URLRequest(url: url!)

        webView!.load(request)
    }

}


<!Doctype html>
<html>
  <head>
    <title>Test Web</title>
    <script>
        window.webkit.messageHandlers.xyz.postMessage("tem")
        function buttonClick1() {
            window.webkit.messageHandlers.xyz.postMessage("tem");
        }

        function myFunction() {

            return "Hi Shubham"
        }




    </script>
    <script src="script.js" ></script>
  </head>
  <body>
    <h3>If you see this then you are working very fine</h1>
    <br />
    <br />
    <br />
    <h3 class="happy">Shubham Bhiwaniwala</h3>
    <h3><a href="support.html" >Support page</a></h3>
    <button onclick="alert('Hi Shubham, how are you?')" >Say Hello</button>
    <button onclick="buttonClick1()" >Call Me</button>
  </body>
</html>


import Foundation
@objcMembers class JavaScriptInterfaceMapping: NSObject {
    func noParam() {
        print("I got called.")
    }

    func oneParam(_ a: String) {
        print("I got \(a)")
    }

    func twoParams(_ first: [Any], _ second: String) {
//        print("I got \(first), \(second)")
        print("I got first item as: \(first[0])")
        print("I got second item as: \(first[1])")
        print(second)
    }
    func threeParams(_ first: [Any], _ second: String, _ third: Bool) {
        //        print("I got \(first), \(second)")
        print("I got first item as: \(first[0])")
        print("I got second item as: \(first[1])")
        print(second)
        print(third)
    }
}

我已经找到了从javascript在iOS中进行方法调用的解决方案。但是我知道,在将javascript与android连接时,我们可以访问javascript中存在于android中的类的对象。

这意味着我们可以直接从javascript调用函数并在android中完成工作。

那么我有什么方法可以在iOS开发中获得相同的功能?

谢谢

0 个答案:

没有答案