最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Opening ViewController when click on button in UIWebView swift - Stack Overflow

programmeradmin0浏览0评论

I Have an app which has UIWebView and the Webview contains simple Button

and here's my 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);
    }
}

And the webView displaying my HTML perfectly

I am just need to present a newViewController from my iOS project when i Click the button in my HTML file ?

like that :

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

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

}



</script>

is there any way to do that in iOS ?

I Have an app which has UIWebView and the Webview contains simple Button

and here's my 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);
    }
}

And the webView displaying my HTML perfectly

I am just need to present a newViewController from my iOS project when i Click the button in my HTML file ?

like that :

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

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

}



</script>

is there any way to do that in iOS ?

Share Improve this question asked Aug 9, 2016 at 11:55 MuhammedMuhammed 5861 gold badge11 silver badges25 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

You can assign a scheme in the js action:

window.location = yourscheme://<scheme_content>

and in swift layer you can use the callback:

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

Yes you can do that like this :

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, pletion:nil)

    
        }
        return true
    }
发布评论

评论列表(0)

  1. 暂无评论