Javascript 单击UIWebView中的按钮时打开ViewController
我有一个应用程序,它有UIWebView,Webview包含简单的按钮 这是我的WebViewController:Javascript 单击UIWebView中的按钮时打开ViewController,javascript,html,ios,swift,webview,Javascript,Html,Ios,Swift,Webview,我有一个应用程序,它有UIWebView,Webview包含简单的按钮 这是我的WebViewController: import UIKit class testViewController: UIViewController { internal static var testID = 0 @IBOutlet weak var myWebView: UIWebView! override func viewDidLoad() { super.vie
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);
}
}
网络视图完美地显示了我的HTML
我只需要在单击HTML文件中的按钮时从iOS项目中显示一个新的ViewController
就像这样:
<button onclick="myFunction()> Submit </button>
<script>
function myFunction()
{
presentViewController('myViewControllerName');
}
</script>
您可以在js操作中分配方案:
window.location = yourscheme://<scheme_content>
是的,您可以这样做:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if (navigationType == UIWebViewNavigationType.FormSubmitted) {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("myViewControllerName") as? myViewControllerName
let navigationController = UINavigationController(rootViewController: VC!)
self.navigationController?.presentViewController(navigationController, animated: true, completion:nil)
}
return true
}
我的xcode错误:使用未声明的类型“myViewControllerName”哈哈大笑-我的xcode错误:使用未声明的类型“myViewControllerName”,因为您需要给出视图控制器名称,而不是“myViewController”,它只是一个示例名称
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if (navigationType == UIWebViewNavigationType.FormSubmitted) {
let VC = self.storyboard?.instantiateViewControllerWithIdentifier("myViewControllerName") as? myViewControllerName
let navigationController = UINavigationController(rootViewController: VC!)
self.navigationController?.presentViewController(navigationController, animated: true, completion:nil)
}
return true
}