IOS JS OC calls each other (JavaScriptCore)

Source: Internet
Author: User

From IOS7 Apple released javascriptcore.framework it makes the interaction between JS and OC more convenient.

Here's a quick look at the framework

First I import the framework

Methods such as the following


Click Linked Frameworks and Libraries after joining to select Javascriptcore.framework


Select Javascriptcore.framework and click on the bottom right corner add join complete

Well, after the creation, we'll import the header file.

#import <JavaScriptCore/JavaScriptCore.h>

You'll see a couple of ways to get in here.


#import "JSContext.h"

#import "JSValue.h"

#import "JSManagedValue.h"

#import "JSVirtualMachine.h"

#import "JSExport.h"


We'll talk about these methods later.


Down we create a uiwebview to test


Create UIWebView

    UIWebView *mywebview;

Initialize, join. Open URL


    Initialize WebView    Mywebview=[[uiwebview alloc]initwithframe:cgrectmake (0, [UIScreen mainscreen]. Bounds.size.width, [UIScreen mainscreen].bounds.size.height-22)];    mywebview.delegate=self;    Add WebView to the current Viewcontroller view    [Self.view Addsubview:mywebview];        URL    nsstring *[email protected] "https://www.baidu.com";    Nsurl *httpurl=[nsurl Urlwithstring:httpstr];    Nsurlrequest *httprequest=[nsurlrequest Requestwithurl:httpurl];    [Mywebview Loadrequest:httprequest];




Perform effects such as the following



Here are some of the proxy methods that we can implement UIWebView

First, let's look at its proxy method.

@protocol uiwebviewdelegate <NSObject> @optional-(BOOL) WebView: (UIWebView *) WebView Shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationType;-(void ) Webviewdidstartload: (UIWebView *) webview;-(void) Webviewdidfinishload: (UIWebView *) webview;-(void) WebView: ( UIWebView *) WebView didfailloadwitherror: (nserror *) error; @end

The effect of each method is to stare in the band.

First we join the agreement

@interface Viewcontroller () <UIWebViewDelegate>

Implementing Proxy methods

#pragma mark--webviewdelegate-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype{    //This method is called before the page is loaded        //retrun YES means that the normal load page returns no will stop page loading    return YES; -(void) Webviewdidstartload: (UIWebView *) webview{    //Start loading Web page Call this method}-(void) Webviewdidfinishload: (UIWebView *) webview{    //page loading complete Call this method}-(void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nserror *) error{    // Page loading failed calling this method}


Every method is when the call is staring inside

Let's first try to invoke the JS method with OC

-(void) Webviewdidfinishload: (UIWebView *) webview{    //page loading complete Call this method        //Create Jscontext First Object (here through the current WebView key gets to Jscontext)    jscontext *context=[webview valueforkeypath:@ " DocumentView.webView.mainFrame.javaScriptContext "];    NSString *[email protected] "alert (' Test JS OC ')"; Ready to run JS code    [Context evaluatescript:alertjs];//invoke JS's alert via OC method    ]

The results of the operation are as follows:




All right. We have implemented the iOS call JS


JS call iOS in the next section

http://blog.csdn.net/lwjok2007/article/details/47058795


Source we upload to the group space [testjsoc.zip] need to download


Apple Development Group: 414319235 Welcome to add welcome discussion questions





IOS JS OC calls each other (JavaScriptCore)

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.