UIWebView often use the jump to the native page. Some may also need to pass a value jump. I wrote a self-defined jump WebView, be able to jump to the specified controller and pass the value, which need to negotiate with the background. How to jump the value in HTML, that is: The Jump button in the HTML associated with the JS method to write.
Writing in HTML
The test code in HTML is as follows:
<! DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><script type="Text/javascript"> //The following is a write dead data directly jump, in principle, the background can dynamically give the corresponding value and by clicking the event value to the native method, to reach the value and jump to the effect of the native page. //controllername The corresponding name is the class name of the native page controller //controllerproperties is a dictionary that includes the corresponding property name and value of the controller above. Equivalent to a key-value pair, which is required to pass the value to the native page //a controller needs to pass the value varControllernamea ="Aviewcontroller";varControllerpropertiesa = {"ProdId":"1001","ProdName":"Vacuum Cleaner","Prodprice":"50 Yuan"};//b Controller needs to pass the value varControllernameb ="Bviewcontroller";varControllerpropertiesb = {"Petid":"1002","PetName":"Lying on the bear.","Petprice":"1 million"};//c controller needs to pass the value varControllernamec ="Cviewcontroller";varControllerpropertiesc = {"PersonId":"1003","PersonName":"Tom","Personsex":"Pervert"};//The Jump method is defined here. Clicking an event runs the following method and passes it to the native method function jumptocontroller(controllername,controllerproperties) {}</script><body bgcolor="#555555"> <!--below are 3 buttons to jump to the corresponding page--<button type="button"onclick="Jumptocontroller (Controllernamea, Controllerpropertiesa) "> Click to go to and jump to a controller</button><button type="button"onclick="Jumptocontroller ( CONTROLLERNAMEB,CONTROLLERPROPERTIESB) "> Click to go to and jump to the B controller</button><button type="button"onclick="Jumptocontroller (Controllernamec, CONTROLLERPROPERTIESC) "> Click to go to and jump to C controller</button></body></html>
The above Jumptocontroller method is the Jump method, click the button will be corresponding to the value of the method and run the contents of the curly braces, the contents of nothing, it is not important. Our goal is simply to pass the value to the method. Native page through JavaScriptCore to know how to run this method and passed the value of enough, because the jump page only need the controller name and the corresponding property values.
On the native page
You need to define 3 controllers first.
Aviewcontroller,bviewcontroller,cviewcontroller. It defines the properties of the test (in fact it should be the first controller, the HTML according to the native page of the detailed class name and attributes to pass the value), here we take the A attribute as an example:
#import <UIKit/UIKit.h>@interface AViewController : UIViewController@property(nonatomicNSString *prodId;@property(nonatomicNSString *prodName;@property(nonatomicNSString *prodPrice;@end
The M file output data in the controller.
Print these properties after entering the page, assuming that there are values coming in at the end. Then the printout is definitely not a null value.
#import "AViewController.h" @interface aviewcontroller ()@end @implementation aviewcontroller - (void) Viewdidload {[SuperViewdidload]; Self. Title= @"A"; Self. View. BackgroundColor= [UicolorGraycolor];NSLog(@"Properties are prodid:%@,prodname:%@,prodprice:%@", Self. ProdId, Self. ProdName, Self. Prodprice);additional setup after loading the view.}
The initial page joins the Jscontext property and loads the HTML file.
Here, load the HTML file that you wrote earlier:
#import "ViewController.h" #import <JavaScriptCore/JavaScriptCore.h> #import <objc/Runtime.h> @interface viewcontroller () <uiwebviewdelegate>@property(nonatomic,Weak)Iboutlet UIWebView*webview;@property(nonatomic,Weak) Jscontext *context;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload]; [ SelfWebviewconfig];additional setup after loading the view, typically from a nib.}/** Set the local page and read it * /- (void) Webviewconfig { Self. Title= @"Initial page";NSString*path = [[NSBundleMainbundle] pathforresource:@"Jstest"oftype:@"HTML"];NSLog(@"%@", path);Nsurl*url = [[NsurlAlloc] Initfileurlwithpath:path];NSLog(@"%@", URL);nsurlrequest*request = [nsurlrequestRequestwithurl:url]; Self. WebView. Delegate= Self; [ Self. WebViewLoadrequest:request];}
/** Set the JS environment. This needs to be read after the Web page * /- (void) Webviewdidfinishload: (UIWebView *) WebView {/** Get the Global JS environment * /Self.context = [WebView valueforkeypath:@"DocumentView.webView.mainFrame.javaScriptContext"];/** Description of the method in the HTML Jumptocontroller how to run * /self.context[@"Jumptocontroller"] = ^{/**args to get this method to pass the number of participants, get the Jsvalue object, and the background to negotiate well. Pass over just a string and a dictionary, a string to put the controller name, a dictionary key and value corresponding to the property name and its value */Nsarray *args = [Jscontext currentarguments]; Class Controllerclass = nil; Nsdictionary *parameters = nil;/** traverse the value from JS, here there are only two, a string a dictionary, the string is the class name, the dictionary is required properties * / for(Jsvalue *jsval in args) {the value passed by the/** is a string, which indicates the controller name . if([JsVal isstring]) {NSString *controllername = [JsVal toString]; Controllerclass = nsclassfromstring (controllername); }The value passed by the/** is the object, indicating that the number of references is in the dictionary. if([JsVal IsObject]) {parameters = [JsVal todictionary]; } }/** get the class name and attributes, directly generate the controller assignment (make sure the string passed is the correct controller class name) */Uiviewcontroller *viewcontroller = (Uiviewcontroller *) [[Controllerclass alloc] init];//Since the correct controller name is already passed. Then there must be a strong success.
/**给控制器赋值*/ [viewController setValuesForKeysWithDictionary:parameters]; /**页面跳转*/ dispatch_async(dispatch_get_main_queue(), ^{ [self.navigationController pushViewController:viewController animated:YES]; }); };}
- A when the interface enters, the printed result is displayed, which is the value passed in the HTML. (Please ignore the interface is not Xcode details).
In principle, the value in HTML is the same as being able to transmit dynamically. That is, the ability to dynamically transfer the value of the jump, the demo can actually be further encapsulated, plus loading bar, as a basic web controller.
Demo:https://github.com/jeffreyww/jfjscoretest
Welcome to the Exchange, qq:25105483.
UIWebView interaction self-defined value jump