Article from: Http://blog.csdn.net/intbird
Compared to Android and JS Interactive, iOS and JS to interact is more convenient;
For Android and JS to interact, see here:
http://blog.csdn.net/intbird/article/details/42295453
Android and JS to interact with the framework, see here:
http://blog.csdn.net/intbird/article/details/46461203
Here to see the interaction between iOS and JS
0. The last ugly picture
1,oc call JS, super convenient, regardless of the code in any location,
Just call stringbyevaluatingjavascriptfromstring and you can
Code:
- (IBAction)webDemo:(id)sender { NSString *name = [[UIDevice currentDevice]name]; NSString *js = [NSString stringWithFormat:@"showMessage(‘%@‘)",name]; [self.webView stringByEvaluatingJavaScriptFromString:js];}
2,js call OC, the main problem is to intercept each URL, the specified schema to intercept the corresponding local method
Code:
-(BOOL) WebView: (nonnullUIWebView*) WebView shouldstartloadwithrequest: (nonnullnsurlrequest*) Request Navigationtype: (Uiwebviewnavigationtype) navigationtype{NSString* URL = Request. URL. absolutestring;if([url hasprefix:@"intbird://"]){NSString*urlconntet = [url substringfromindex:@"intbird://". Length];Nsarray*urlparams = [Urlconntet componentsseparatedbystring:@"/"];NSString* methodname = urlparams[0];NSString* param = urlparams[1];if([MethodName isequaltostring:@"Democallmethod"]){ [ SelfPerformselector:@selector(Democallmethod:) Withobject:param]; }//# pragma clang diagnostic ignored "-warc-performselector-leaks" //sel method = nsselectorfromstring (methodname); //[self Performselector:method Withobject:param]; return NO; }return YES;}
4, important Viewcontroller code:
////VIEWCONTROLLER.M//Intbirdjsinterface////Created by Intbird on 15/8/31.//Copyright 2015 Intbird. All rights reserved.//#import "ViewController.h" #import <JavaScriptCore/JavaScriptCore.h> @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];Nsurl*url = [Nsurlurlwithstring:@"http://127.0.0.1"];nsurlrequest* request= [nsurlrequestRequestwithurl:url]; [_webview Loadrequest:request]; _webview. Delegate= Self;}//oc call JS;- (ibaction) Webdemo: (ID) Sender {NSString*name = [[Uidevice currentdevice]name];NSString*js = [NSStringstringwithformat:@"ShowMessage ('%@ ')", name]; [ Self. WebViewSTRINGBYEVALUATINGJAVASCRIPTFROMSTRING:JS];}//js call OC;-(BOOL) WebView: (nonnullUIWebView*) WebView shouldstartloadwithrequest: (nonnullnsurlrequest*) Request Navigationtype: (Uiwebviewnavigationtype) navigationtype{NSString* URL = Request. URL. absolutestring;if([url hasprefix:@"intbird://"]){NSString*urlconntet = [url substringfromindex:@"intbird://". Length];Nsarray*urlparams = [Urlconntet componentsseparatedbystring:@"/"];NSString* methodname = urlparams[0];NSString* param = urlparams[1];if([MethodName isequaltostring:@"Democallmethod"]){ [ SelfPerformselector:@selector(Democallmethod:) Withobject:param]; }//# pragma clang diagnostic ignored "-warc-performselector-leaks" //sel method = nsselectorfromstring (methodname); //[self Performselector:method Withobject:param]; return NO; }return YES;} -(void) Democallmethod: (NSObject*) message{uialertcontroller * alert = [Uialertcontroller alertcontrollerwithtitle:@"Democallmethod"Message: (NSString*) message Preferredstyle:uialertcontrollerstylealert]; Uialertaction * Alertcacel = [Uialertaction actionwithtitle:@"Cacel"Style:uialertactionstylecancel handler:Nil]; [Alert Addaction:alertcacel]; [ SelfPresentviewcontroller:alert Animated:YESCompletion:Nil];} -(void) WebView: (nonnullUIWebView*) WebView Didfailloadwitherror: (NullableNserror*) error{//jscontext *context = [WebView valueforkey:@ "DocumentView.webView.mainFrame.javaScriptContext"]; //NSString *ALERTSTR = @ "alert (' Alert called by OC ')"; //[context Evaluatescript:alertstr];}-(void) Webviewdidfinishload: (nonnullUIWebView*) webview{}-(void) Webviewdidstartload: (nonnullUIWebView*) webview{}-(void) Webgoback: (ID) sender{if([_webview CanGoBack]) {[_webview goBack]; }}-(void) Webreload: (ID) sender{[_webview reload];}@end
5: Nonsense not much to say, directly on the demo:
Https://github.com/intbird/IOSIntbirdJsInterface
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ios:ios and JavaScript call OC and JS with each other