IOS oc interaction with JS (JavaScriptCore implementation)

Source: Internet
Author: User

This article includes JS call OC method and pass value, OC Call JS method and pass Value

Originally wanted to put HTML into the server, and then access, but think if the HTML in the local loading more helpful to understand, put HTML into the project

HTML code
<! DOCTYPE Html>"UTF-8">"margin-top:20px">"Button"Value="Invoke Local method (call)"onclick="Tianbai.call ()"></div><div><input type="Button"Value="evoke Getcall: (NSString *) callstring Pass value"onclick="Call ()"></div><script>var call=function () {var callinfo= Json.stringify ({"Jianshu":"Http://www.jianshu.com/users/55c8fdc3c6e7/latest_articles"}); Tianbai.getcall (callinfo);} var Callback=function (str) {alert (str);} var alercallback=function () {alert ('Success');}</script></body>

The code for the above HTML: two buttons created

The first button binds the tianbai.call() method, and here tianbai is an object that is described in the following OC code to invoke the tianbai.call() method on behalf of the tianbai object call()

The second button binds the method that call() calls the method in the following JavaScript, which call() in JavaScript call() defines a callInfo parameter that represents the object in the method tianbai.getCall(callInfo) tianbai called method and pass parameters callInfo , the following two methods are OC call JavaScript method, where callback returns str,alercallback for OC only call JavaScript Method!

OC Code

Demo using native JavaScriptCore class

Introduction of three nouns:

    1. Jscontext: Provides a context for JavaScript to run
    2. Bridges of Jsvalue:javascript and objective-c data and methods
    3. Jsexport: This is an agreement that, if you interact with a protocol, your own defined protocol must comply with this Protocol

Code in the ViewController.h

////ViewController.h//JavaScript////Created by Tianbai on 16/6/8.//Copyright 2016 Xiamen B branch network company. All rights reserved.//#import<UIKit/UIKit.h>#import<JavaScriptCore/JavaScriptCore.h>@protocolJsobjcdelegate <JSExport>
- (void) call;- (void) Getcall: (NSString *) callstring;@end

@interfaceViewcontroller:uiviewcontroller<uiwebviewdelegate, jsobjcdelegate>

@property (nonatomic, strong) Jscontext*Jscontext, @property (Strong, nonatomic) UIWebView*WebView;@end

Code in the VIEWCONTROLLER.M

////VIEWCONTROLLER.M//JavaScript////Created by Tianbai on 16/6/8.//Copyright 2016 Xiamen B branch network company. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }
- (void) Viewdidappear: (BOOL) Animated {
Self.webview= [[UIWebView alloc]initwithframe:cgrectmake (0, -, [UIScreen mainscreen].bounds.size.width, [UIScreen Mainscreen].bounds.size.height]]; Self.webview.Delegate=Self ; NSString* Path = [[NSBundle mainbundle] Pathforresource:@"Index"OfType:@"HTML"]; Nsurl* URL =[Nsurl Fileurlwithpath:path]; Nsurlrequest* Request =[Nsurlrequest Requestwithurl:url]; [Self.webview Loadrequest:request]; [Self.view AddSubview:self.webView];}
- (void) Webviewdidfinishload: (UIWebView *) WebView {
Self.jscontext= [WebView Valueforkeypath:@"DocumentView.webView.mainFrame.javaScriptContext"]; self.jscontext[@"Tianbai"] =Self ; Self.jsContext.exceptionHandler= ^ (Jscontext *context, Jsvalue *Exceptionvalue) {
Context.exception=Exceptionvalue; NSLog (@"exception information:%@", Exceptionvalue); };}- (void) Call {
NSLog (@"Pager"); //then in the callback JS method callback the content to pass outJsvalue *callback = self.jscontext[@"Callback"]; //Pass the value to the Web side[Callback callwitharguments:@[@"Invoke local OC callback complete"]];}- (void) Getcall: (NSString *) callstring {
NSLog (@"get:%@", callstring); //the method of successful callback JS callbackJsvalue *callback = self.jscontext[@"Alercallback"]; [Callback Callwitharguments:nil]; //Add a prompt box directly//nsstring *str = @ "alert (' OC added JS hint succeeded ')";//[Self.jscontext evaluatescript:str];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

IOS oc interaction with JS (JavaScriptCore implementation)

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.