Reprint IOS JS OC calls each other (JavaScriptCore)

Source: Internet
Author: User

 IOS JS OC calls each other (JavaScriptCore)

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.

[OBJC]View PlainCopy
    1. #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

[OBJC]View PlainCopy
    1. UIWebView *mywebview;

Initialize, add, open URLs

[OBJC]View PlainCopy
  1. Initialize 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 WebView to the current Viewcontroller view
  5. [self. View Addsubview:mywebview];
  6. Url
  7. NSString *httpstr=@ "https://www.baidu.com";
  8. Nsurl *httpurl=[nsurl Urlwithstring:httpstr];
  9. Nsurlrequest *httprequest=[nsurlrequest Requestwithurl:httpurl];
  10. [Mywebview loadrequest:httprequest];


Run the following effect

Let's implement several proxy methods for UIWebView

First, let's look at its proxy method.

[OBJC]View PlainCopy
  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

[OBJC]View PlainCopy
    1. @interface Viewcontroller () <UIWebViewDelegate>

Implementing Proxy methods

[OBJC]View PlainCopy
  1. #pragma Mark--webviewdelegate
  2. -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype
  3. {
  4. This method is called before the page is loaded
  5. //retrun YES indicates normal load page return no will stop page loading
  6. return YES;
  7. }
  8. -(void) Webviewdidstartload: (UIWebView *) WebView
  9. {
  10. //Start loading Web page Call this method
  11. }
  12. -(void) Webviewdidfinishload: (UIWebView *) WebView
  13. {
  14. //Page load Complete Call this method
  15. }
  16. -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (nserror *) error
  17. {
  18. //Page load Failure Call this method
  19. }


Each method is when the call is in the comment

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

[OBJC]View PlainCopy
  1. -(void) Webviewdidfinishload: (UIWebView *) WebView
  2. {
  3. //Page load Complete Call this method
  4. //Create the Jscontext object first (here through the current WebView key to get to Jscontext)
  5. jscontext *context=[webview valueforkeypath:@ "DocumentView.webView.mainFrame.javaScriptContext"];
  6. nsstring *alertjs=@ "alert (' Test JS OC ')"; //Ready to execute JS code
  7. [Context Evaluatescript:alertjs]; //Invoke JS's alert via OC method
  8. }

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

Reprint 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.