IOS interacts with JS

Source: Internet
Author: User
1. Through UIWebView and JS interaction

Native and server interaction through Web pages so easy. Of course, to achieve the delegate of WebView. Self.webView.delegate = self;


1.1 iOS Client Calls JS method.

Directly using the method of the system Stringbyevaluatingjavascriptfromstring call JS method can be.

[_webview stringbyevaluatingjavascriptfromstring:@ "Smallfont ()"];

* Note: Smallfont () is the method of JS.


1.2 js to call iOS method.

Compared to Android, iOS is more cumbersome to call, because Android has a way to be called, and iOS is more reserved, it is not directly to be called

1. First we have to drink the server side to define a good method and logo. For example, the server definition of the js2ios://{"functionname": "BuyNow", "args": "88888888"} means that my parameters in the band Js2ios, I want to interact with you. You give me a way to achieve buynow ( functionname) I'm going to use the parameter (args) 88888888 to call.

2. Because iOS can only be in the URL redirect when the implementation is JS call, so the premise is that I click on the page to interact with the button and so on, to be able to jump into my

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype method, if you do not enter, you can tell the server's brother said: Hey, handsome, your code may be a bit of a problem, if you make sure you realize the WebView delegate, which is Self.webView.delegate = self;

3. We are now in-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) navigationtype to intercept the URL.

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {

if ([Request. Url.scheme isequaltostring:@ "Js2ios"]) {

Decode

NSString *DECODEURLSTR1 = [Request. Url.absolutestring stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

NSString *decodeurlstr;

SEL Selctor = nsselectorfromstring (the method name to be intercepted);

IMP imp = [self methodforselector:selctor];

void (*func) (Id,sel) = (void *) imp;

Func (Self,selctor);

[Self performselector:selctor withobject:nil];

The method that calls the annotation directly here has a warning that you can refer to: My article <ios performselector may cause a leak because it selector is unknown>

Of course, if you do not want to load a specific URL, or load a specific URL to do special processing, you can intercept here

}

return YES;

}




2. Run-time Interaction

Study hard ...

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.