Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening ViewController when click on button in UIWebView swift

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 ?

like image 680
Muhammed Avatar asked Apr 05 '26 18:04

Muhammed


2 Answers

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;
}
like image 138
Marco Santarossa Avatar answered Apr 08 '26 08:04

Marco Santarossa


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

    
        }
        return true
    }
like image 42
Anjali Bhimani Avatar answered Apr 08 '26 06:04

Anjali Bhimani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!