Coin163

首页 > webview和js的交互

webview和js的交互

相关标签: webview js

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

相关推荐:Android WebView 与JS的数据交互

关于WebView 我们知道目前android市场上的一些应用采用的开发方式大致分为三种:Native App、Web App、Hybrid App。本文主要是Hybrid App中实现的主要技术native组件与js的数据交互的理解以及实现。   Android API中提供了WebView组件来实现对html的渲染。所

https://github.com/marcuswestin/WebViewJavascriptBridge


用法摘抄如下:

引用 See ExampleAppDelegate.* for example  code. To use it in your own project:

1) Copy Classes/WebViewJavascriptBridge.h and Classes/WebViewJavascriptBridge.m into your Xcode project

2) Instantiate a UIWebView, a WebViewJavascriptBridge, and set yourself as the bridge's delegate

#import <UIKit/UIKit.h>
#import "WebViewJavascriptBridge.h"

@interface ExampleAppDelegate : UIResponder <UIApplicationDelegate, WebViewJavascriptBridgeDelegate>

@end

@implementation ExampleAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti*****:(NSDictionary *)launchOpti*****
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.webView = [[UIWebView alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:webView];
    self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeWithDelegate:self];
    self.webView.delegate = javascriptBridge;

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)javascriptBridge:(WebViewJavascriptBridge *)bridge receivedMessage:(NSString *)message fromWebView:(UIWebView *) webView 
{
    NSLog(@"MyJavascriptBridgeDelegate received message: %@", message);
}

@end
3) Go ahead and send some messages from Objc to javascript

[self.javascriptBridge sendMessage:@"Well hello there" toWebView:self.webView];
4) Finally, set up the javascript side of things

document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady() {
    WebViewJavascriptBridge.setMessageHandler(function(message) {
        alert('Received message: ' + message)
    })
    WebViewJavascriptBridge.sendMessage('Hello from the javascript')
}, false)

原文

https://github.com/marcuswestin/WebViewJavascriptBridge 用法摘抄如下: 引用 See ExampleAppDelegate.* for example  code. To use it in your own project: 1) Copy Classes/WebViewJavasc

------分隔线----------------------------