Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I call an Objective-C method from Javascript which is loaded in web page?

    I have an IOS application which has a web browser in it.

    When this web browser is displayed, it navigates to a page on Internet, say http://something.com:8080/success.php

    Is it possible to place a javascript code on this web site, which communicates with the native application? Can it call an Objective-C method?

    I tried this, but didn't work.

    On Javascript side:

    Your javascript code will navigate to a 'fake' URL that you will filter on the method above. Something like:

    window.location = "js2objc://savearam";

  2. #2
    Regular Coder
    Join Date
    Sep 2014
    Posts
    191
    Thanks
    0
    Thanked 34 Times in 32 Posts
    Check WKWebView.

    https://developer.apple.com/reference/webkit/wkwebview.

    Execute the following from your JavaScript:

    Code:
    /*======================================================================================
      		send IPhone, native app WKWebView
      ======================================================================================*/
      function main_PostMsgToWKWebView(obj) {
    	try {
    		var message = JSON.stringify(obj);
        	window.webkit.messageHandlers.observe.postMessage(message);
    	}
    	catch(err) {
    		//alert("todo: mainrtns.js line 100: post failed");
    	}
      }
    It will be received by

    Code:
    -(void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    //message.body
    }
    See html - How to call a Javascript method in iOS? - Stack Overflow


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •