IOS-JS interaction (WebViewJavascriptBridge), iosjsbridge

Source: Internet
Author: User

IOS-JS interaction (WebViewJavascriptBridge), iosjsbridge
//
// ViewController. m
// JSAndIOS Interaction
//
// Created by apple on 15/7/31.
// Copyright (c) 2015 tqh. All rights reserved.
//

# Import "ViewController. h"
# Import "WebViewJavascriptBridge. h"
@ Interface ViewController () <UIWebViewDelegate>
@ Property WebViewJavascriptBridge * bridge;
@ End

@ Implementation ViewController

-(Void) viewDidLoad {
[Super viewDidLoad];
If (_ bridge) {return ;}

UIWebView * webView = [[UIWebView alloc] initWithFrame: self. view. bounds];
[Self. view addSubview: webView];

[WebViewJavascriptBridge enableLogging];

// Webpage interaction. The webpage sends a message to the program
_ Bridge = [WebViewJavascriptBridge bridgeForWebView: webView webViewDelegate: self handler: ^ (id data, WVJBResponseCallback responseCallback ){
NSLog (@ "received message from JS: % @", data );
ResponseCallback (@ "back ");
}];
// Webpage interaction. The webpage sends a message to the program
[_ Bridge registerHandler: @ "testObjcCallback" handler: ^ (id data, WVJBResponseCallback responseCallback ){
NSLog (@ "testObjcCallback called: % @", data );
ResponseCallback (@ "back ");
}];
[Self renderButtons: webView];
[Self loadExamplePage: webView];
}

-(Void) renderButtons :( UIWebView *) webView {
UIFont * font = [UIFont fontWithName: @ "HelveticaNeue" size: 12.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 (10,414,100, 35 );
MessageButton. titleLabel. font = font;
MessageButton. backgroundColor = [UIColor colorWithWhite: 1 alpha: 0.75];

UIButton * callbackButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[CallbackButton setTitle: @ "Call handler" forState: UIControlStateNormal];
[CallbackButton addTarget: self action: @ selector (callHandler :) forControlEvents: UIControlEventTouchUpInside];
[Self. view insertSubview: callbackButton aboveSubview: webView];
CallbackButton. frame = CGRectMake (110,414,100, 35 );
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 (210,414,100, 35 );
ReloadButton. titleLabel. font = font;
}


// Load the webpage
-(Void) loadExamplePage :( UIWebView *) webView {
// ExampleApp loads the local webpage
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];
}

# Pragma mark-event listening

-(Void) sendMessage :( id) sender {
// Send a message
[_ Bridge send: @ "oc send message" responseCallback: ^ (id response ){
NSLog (@ "sendMessage got response: % @", response );
}];
}

-(Void) callHandler :( id) sender {
// Send a message
Id data =@{@ "name": @ "value "};
[_ Bridge callHandler: @ "testJavascriptHandler" data: data responseCallback: ^ (id response ){
NSLog (@ "testJavascriptHandler responded: % @", response );
}];
}

# Pragma mark-UIWebViewDelegate

-(Void) webViewDidStartLoad :( UIWebView *) webView {
NSLog (@ "webViewDidStartLoad ");
}

-(Void) webViewDidFinishLoad :( UIWebView *) webView {
NSLog (@ "webViewDidFinishLoad ");
}
@ End

Github download: https://github.com/marcuswestin/WebViewJavascriptBridge

Baidu download: http://pan.baidu.com/s/1kTnbpTd

Js learning: http://www.w3school.com.cn/

Webkit learning: http://blog.csdn.net/column/details/webkit-in-deep.html

Related Article

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.