在UIWebView swift中单击按钮时打开ViewController

Muh*_*med 3 html javascript webview ios swift

我有一个拥有UIWebView的应用程序,而Webview包含简单的Button

这是我的WebViewController:

import UIKit

class testViewController: UIViewController {
    internal static var testID = 0

    @IBOutlet weak var myWebView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let url = NSURL (string: "http://mydomainname/index.html");
        let requestObj = NSURLRequest(URL: url!);
        myWebView.loadRequest(requestObj);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且webView完美地显示了我的HTML

当我单击HTML文件中的按钮时,我只需要从我的iOS项目中呈现一个newViewController?

像那样 :

<button onclick="myFunction()> Submit </button>

<script>
function myFunction()
{
presentViewController('myViewControllerName');

}



</script>
Run Code Online (Sandbox Code Playgroud)

在iOS中有什么办法吗?

Mar*_*ssa 6

您可以在js操作中分配方案:

window.location = yourscheme://<scheme_content>
Run Code Online (Sandbox Code Playgroud)

在swift层中你可以使用回调:

func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    if (request.URL.scheme == "yourscheme") {
        // Your code
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)