//
//VIEWCONTROLLER.M
//Jsandios Interaction
//
//Created by Apple on 15/7/31.
//Copyright (c) 2015 TQH. All rights reserved.
//
#import "ViewController.h"
#import"WebViewJavascriptBridge.h"
@interfaceViewcontroller () <UIWebViewDelegate>
@property webviewjavascriptbridge* Bridge;
@end
@implementationViewcontroller
- (void) Viewdidload {
[Super Viewdidload];
if(_bridge) {return; }
uiwebview* WebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[Self.view Addsubview:webview];
[Webviewjavascriptbridge enableLogging];
//Web pages interact, Web pages send messages to Programs
_bridge = [Webviewjavascriptbridge bridgeforwebview:webview webviewdelegate:self handler:^ (IDData, Wvjbresponsecallback Responsecallback) {
NSLog (@"receive Message from JS:%@", data);
Responsecallback (@" Back");
}];
//Web pages interact, Web pages send messages to Programs
[_bridge Registerhandler:@"Testobjccallback"handler:^ (IDData, Wvjbresponsecallback Responsecallback) {
NSLog (@"Testobjccallback called:%@", data);
Responsecallback (@" Back");
}];
[Self renderbuttons:webview];
[Self loadexamplepage:webview];
}
- (void) Renderbuttons: (uiwebview*) WebView {
uifont* font = [Uifont fontwithname:@"Helveticaneue"Size12.0];
UIButton *messagebutton = [UIButton buttonwithtype:uibuttontyperoundedrect];
[Messagebutton Settitle:@"Send Message"Forstate:uicontrolstatenormal];
[Messagebutton addtarget:self Action: @selector (sendMessage:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Insertsubview:messagebutton Abovesubview:webview];
Messagebutton.frame = CGRectMake (Ten,414, -, *);
MessageButton.titleLabel.font = font;
Messagebutton.backgroundcolor = [Uicolor colorwithwhite:1Alpha0.75];
UIButton *callbackbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];
[Callbackbutton Settitle:@"Invoking Handlers"Forstate:uicontrolstatenormal];
[Callbackbutton addtarget:self Action: @selector (Callhandler:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Insertsubview:callbackbutton Abovesubview:webview];
Callbackbutton.frame = CGRectMake ( the,414, -, *);
CallbackButton.titleLabel.font = font;
uibutton* Reloadbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];
[Reloadbutton Settitle:@"Reload WebView"Forstate:uicontrolstatenormal];
[Reloadbutton Addtarget:webview Action: @selector (Reload) forcontrolevents:uicontroleventtouchupinside];
[Self.view Insertsubview:reloadbutton Abovesubview:webview];
Reloadbutton.frame = CGRectMake ( About,414, -, *);
ReloadButton.titleLabel.font = font;
}
//Loading Web pages
- (void) Loadexamplepage: (uiwebview*) WebView {
//exampleapp loading local web pages
nsstring* Htmlpath = [[NSBundle mainbundle] Pathforresource:@"Exampleapp"OfType:@"HTML"];
nsstring* apphtml = [NSString stringwithcontentsoffile:htmlpath encoding:nsutf8stringencoding Error:nil];
Nsurl *baseurl = [Nsurl Fileurlwithpath:htmlpath];
[WebView loadhtmlstring:apphtml Baseurl:baseurl];
}
#pragmaMark-Event Monitoring
- (void) SendMessage: (ID) Sender {
//Send Message
[_bridge Send:@"OC Send Message"responsecallback:^ (IDResponse) {
NSLog (@"SendMessage got response:%@", response);
}];
}
- (void) Callhandler: (ID) Sender {
//Send Message
IDdata = @{@"name":@"value"};
[_bridge Callhandler:@"Testjavascripthandler"Data:data responsecallback:^ (IDResponse) {
NSLog (@"Testjavascripthandler responded:%@", response);
}];
}
#pragmaMark-uiwebviewdelegate
- (void) Webviewdidstartload: (UIWebView *) WebView {
NSLog (@"Webviewdidstartload");
}
- (void) Webviewdidfinishload: (UIWebView *) WebView {
NSLog (@"Webviewdidfinishload");
}
@end
GitHub Download: Https://github.com/marcuswestin/WebViewJavascriptBridge
Baidu Cloud Download: http://pan.baidu.com/s/1kTnbpTd
JS Learning: http://www.w3school.com.cn/
Http://www.fenesky.com/blog/2014/04/24/how-javascriptcore-works.html
Http://www.cnblogs.com/ider/p/introduction-to-ios7-javascriptcore-framework.html
WebKit Study: http://blog.csdn.net/column/details/webkit-in-deep.html
Ios-js Interaction (Webviewjavascriptbridge)