from iOS7 Start Apple announced JavaScriptCore.framework It makes the JS And OC It's more convenient to interact with .

Let's take a brief look at this framework

First I import framework

The method is as follows

Click on Linked Frameworks and Libraries After adding choice JavaScriptCore.framework

Choose JavaScriptCore.framework after Click on the bottom right corner Add Add to complete

good After the creation, let's import the header file

  1. #import <JavaScriptCore/JavaScriptCore.h>

Click in You will see the following methods

#import "JSContext.h"

#import "JSValue.h"

#import "JSManagedValue.h"

#import "JSVirtualMachine.h"

#import "JSExport.h"

We'll talk about these methods later

Now let's create a UIWebView   For testing

establish UIWebView

  1. UIWebView *myWebView;

initialization , add to , Open the url

  1. // initialization webview
  2. myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 22, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-22)];
  3. myWebView.delegate=self;
  4. // add to webview To the current viewcontroller Of view On
  5. [self.view addSubview:myWebView];
  6. // website
  7. NSString *httpStr=@"";
  8. NSURL *httpUrl=[NSURL URLWithString:httpStr];
  9. NSURLRequest *httpRequest=[NSURLRequest requestWithURL:httpUrl];
  10. [myWebView loadRequest:httpRequest];

The operation effect is as follows

Let's implement UIWebView Several proxy methods of agent

First, let's look at its proxy method

  1. @protocol UIWebViewDelegate <NSObject>
  2. @optional
  3. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
  4. - (void)webViewDidStartLoad:(UIWebView *)webView;
  5. - (void)webViewDidFinishLoad:(UIWebView *)webView;
  6. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
  7. @end

The function of each method will be annotated in the band

First, we add the protocol

  1. @interface ViewController ()<UIWebViewDelegate>

Implementing agent methods

  1. #pragma mark --webViewDelegate
  2. -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  3. {
  4. // This method is called before the page loads
  5. //retrun YES  Indicates that the web page is loaded normally   return NO  Will stop page loading
  6. return YES;
  7. }
  8. -(void)webViewDidStartLoad:(UIWebView *)webView
  9. {
  10. // Start loading the web page and call this method
  11. }
  12. -(void)webViewDidFinishLoad:(UIWebView *)webView
  13. {
  14. // Call this method after loading the web page
  15. }
  16. -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  17. {
  18. // Page loading failed   Call this method
  19. }

When each method is called is in the comment

Let's try it out first oc Call js Method

  1. -(void)webViewDidFinishLoad:(UIWebView *)webView
  2. {
  3. // Call this method after loading the web page
  4. // First create JSContext  object ( Here through the current webView Key to get jscontext)
  5. JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
  6. NSString *alertJS=@"alert('test js OC')"; // Ready for execution js Code
  7. [context evaluateScript:alertJS];// adopt oc Method call js Of alert
  8. }

The implementation effect is as follows :

Okay , We've done that iOS call js

JavaScriptCore.framework Basic usage ( One ) More articles about

  1. JavaScriptCore.framework Basic usage ( Two )

    And then what we talked about in the last section iOS call js Let's use js call iOS js call iOS There are two situations One ,js It calls the method directly Two ,js It calls methods through objects First of all, let's look at the first one , Direct call method . Which USES iOS Of bl ...

  2. ios7 JavaScriptCore.framework

    Once wanted to oc call js The code is usually through uiwebview Of stringByEvaluatingJavaScriptFromString Method , Or use it directly JavaScriptCore engine , such as https:/ ...

  3. iOS in JS And OC Interaction (JavaScriptCore.framework)

    iOS To realize js And oc Interaction , At present, there are many popular open source solutions on the Internet : Such as :react native Of course, some lightweight tasks are provided by the system UIWebView as well as JavaScriptCore.framewo ...

  4. iOS Development :JavaScriptCore.framework Simple use --JS And OC The interaction of

    iOS7 And then Apple launched for apes JavaScriptCore.framework This framework , This framework is for you to work with JS It's very helpful interactively , Can be in html Call... On the interface OC Methods and pass the reference , It can also be in OC On the call JS Methods and pass on ...

  5. device framework( Equipment frame )

    Table A-1  Device frameworks Name First available Prefixes Description Accelerate.framework 4.0 cbla ...

  6. [ turn ]JavaScriptCore by Example

    original text : JavaScriptCore is not a new framework; i ...

  7. [ turn ]JavaScriptCore and iOS 7

    original text : As a rule, iOS programmers don't think ...

  8. [ turn ]Running JavaScript in an iOS application with JavaScriptCore

    original text : ...

  9. OC and JS Interaction ---JavaScriptCore

    JavaScriptCore A brief introduction JavaScriptCore yes iOS7 The native framework provided , It can make Objective-C and JavaScript Provide simple and direct interaction . JavaScriptCore Use ...

Random recommendation

  1. javascript Data structure and algorithm --- queue

    javascript Data structure and algorithm --- queue A queue is a list , The difference is that the queue can only insert elements at the end of the queue , Delete elements at the head of the team . Queues are used to store data in order , fifo , This is not the same as the stack ( After the first out ). In the stack , Last element to stack ...

  2. 【poj1080】 Human Gene Functions ( Topic link ) The question Give two that contain only letters ACGT String s1.s2, You can insert characters in two strings “-”, bring s1 And s2 It's the most similar . Solu ...

  3. php Common interview questions

    1. If it's not on cookies,session How to work ? PHP Medium sessions You usually use cookies Methods . But if not cookies( Browser disable cookies),PHP sessions also ...

  4. Redis Transactions and watch

    redis The business of Strictly speaking ,redis And the traditional database we understand ( Such as mysql) My business is different . redis Transaction definition in Redis The transaction (transaction) It's a set of commands . Business is the same as ...

  5. .net core High performance communication open source components BeetleX

    BeetleX beetleX Is based on dotnet core To achieve lightweight high performance TCP Communication components , Easy to use . High performance, safety and reliability are the starting point of component design ! Developers can Beetlx With the support of components, we can quickly build high-performance T ...

  6. Delete an attribute of an object delete

    Sometimes we may encounter the situation that we need to delete an attribute of an object , Keep the rest , I don't want to traverse , Then we can use delete The operator , let obj = { a: 1, b: 2, c: 3 } delete obj ...

  7. python unittest frame

    Four parts : test fixture A test fixture represents( representative ) the preparation needed to perform one or more tests ...

  8. Easyui Learn to organize your notes

    Catalog (1) Table double click event (2) treegrid Send parameter update (3) Settings list checkbox The radio (4) Easyui Realize single line selection and deselection @ I'm interested in easyui Take notes , Update from time to time ( ...

  9. init_ir_ Technology implementation

    1. Interrupt initialization The interrupt vector number is 8 Bit , So it has a total of 256 term (0-255), So the most interrupt descriptor table is 256 term , So is the interrupt vector table 256 term . For different interrupts , During interrupt initialization and interrupt processing , The treatment is different ...

  10. Digital Twin Of 8 Kind of interpretation !

    The international 8 Major mainstream manufacturers digital twin The understanding of the , It's necessary to make a summary ! According to the IDC forecast ,2017 There will be 40% Most of the large manufacturers use virtual simulation technology to model their production process ,Digital Twin Sure ...