我有一个拥有UIWebView的应用程序,而Webview包含简单的按钮
这是我的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);
}
}
webView完美显示我的HTML
当我单击HTML文件中的按钮时,我只需要从我的iOS项目中呈现一个newViewController?
像那样:<button onclick="myFunction()> Submit </button>
<script>
function myFunction()
{
presentViewController('myViewControllerName');
}
</script>
有没有办法在iOS中做到这一点?
答案 0 :(得分:6)
您可以在js操作中指定方案:
window.location = yourscheme://<scheme_content>
在swift层中你可以使用回调:
func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
if (request.URL.scheme == "yourscheme") {
// Your code
}
return true;
}
答案 1 :(得分:2)
是的,你可以这样做:
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
}