How does JavaScript call the objective-C method?

Source: Internet
Author: User
Example ~ To implement
Interaction between JavaScript and objc

How does one implement JavaScript to call an interface inside objc?

I implemented it in this way.

For example, an entry
<Input type = "button" id = "enter" value = "enter" onclick = "testclick ('printlog1: log2: ', 'ddddd', 'aaaaa '); "/>

JS
Function testclick (CMD, parameter1, parameter2)
{
Alert (1 );
Document. Write (date ());
Window. Location. href = "objc: //" + cmd + ":/" + parameter1 + ":/" + parameter2;
}

Note: window. location. href = "objc: //" + cmd + ":/" + parameter1 + ":/" + parameter2; // cmd indicates that the method name must not contain spaces, the latter is the attached Parameter
The parameter format depends on your needs.


-(Bool) webview :( uiwebview *) webview shouldstartloadwithrequest :( nsurlrequest *) Request navigationtype :( uiwebviewnavigationtype) navigationtype
{
Nsstring * urlstring = [[request URL] absolutestring];

Nsarray * urlcomps = [urlstring componentsseparatedbystring: @ ": //"];

If ([urlcomps count] & [[urlcomps objectatindex: 0] isequaltostring: @ "objc"])
{

Nsarray * arrfucnameandparameter = [(nsstring *) [urlcomps objectatindex: 1] componentsseparatedbystring: @ ":/"];
Nsstring * funcstr = [arrfucnameandparameter objectatindex: 0];

If (1 = [arrfucnameandparameter count])
{
// No Parameters
If ([funcstr isequaltostring: @ "dofunc1"])
{

/* Call the local function 1 */
Nslog (@ "dofunc1 ");

}
}
Else
{
// Parameters
If ([funcstr isequaltostring: @ "printlog1: log2:"])
{
[Self printlog1: [arrfucnameandparameter objectatindex: 1] log2: [arrfucnameandparameter objectatindex: 2];
}
}
Return no;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.