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
- #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
- UIWebView *mywebview;
Initialize, add, open URLs
[OBJC]View PlainCopy
- Initialize WebView
- Mywebview=[[uiwebview Alloc]initwithframe:cgrectmake (0, 22, [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 *httpstr=@ "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.
[OBJC]View PlainCopy
- @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
[OBJC]View PlainCopy
- @interface Viewcontroller () <UIWebViewDelegate>
Implementing Proxy methods
[OBJC]View PlainCopy
- #pragma Mark--webviewdelegate
- -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype
- {
- This method is called before the page is loaded
- //retrun YES indicates normal load page return no will stop page 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 Failure Call this method
- }
Each method is when the call is in the comment
Let's first try to invoke the JS method with OC
[OBJC]View PlainCopy
- -(void) Webviewdidfinishload: (UIWebView *) WebView
- {
- //Page load Complete Call this method
- //Create the Jscontext object first (here through the current WebView key to get to Jscontext)
- jscontext *context=[webview valueforkeypath:@ "DocumentView.webView.mainFrame.javaScriptContext"];
- nsstring *alertjs=@ "alert (' Test JS OC ')"; //Ready to execute JS code
- [Context Evaluatescript:alertjs]; //Invoke JS's alert via OC method
- }
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)