IOS JS OC calls each other (JavaScriptCore)

Source: Internet
Author: User

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

Let's take a quick look at this framework

First I import the framework

Methods are as follows


Click Linked Frameworks and Libraries after adding the option Javascriptcore.framework


Select Javascriptcore.framework and click on the bottom right corner add to finish

OK, we'll import the header file after the creation is complete.

#import <JavaScriptCore/JavaScriptCore.h>

You can see a few 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, add, open URLs


    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];




Run the following effect



Let's implement several proxy methods for 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 function of each method will be annotated in the band.

First we add the protocol

@interface Viewcontroller () <UIWebViewDelegate>

Implementing Proxy methods

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


Each method is when the call is in the comment

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

-(void) Webviewdidfinishload: (UIWebView *) webview{    //page load 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 execute the JS code    [Context evaluatescript:alertjs];//by the OC method call JS Alert    }

The following results are performed:




Well, we've implemented the iOS call JS


JS call iOS in the next section

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


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


Apple Development Group: 414319235 Welcome to join the Welcome discussion question





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.