Ios-js Interaction (Webviewjavascriptbridge)

Source: Internet
Author: User

//
//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)

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.