// 这段代码是固定的,必须要放到js中
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge);
}
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback);
}
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none'; WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'; document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
document.getElementById('share').onclick=function(url,imgurl,title,message){ console.log(url,imgurl,title,message)
if(ua==1){ //安卓回调
window.WebViewJavascriptBridge.callHandler('nativeShare', {'url':url,'title':title,'message':message,'imgurl':imgurl}, function(responseData) {});
}else{ //ios回调
setupWebViewJavascriptBridge(function(bridge){
bridge.callHandler('share', {'url':url,'title':title,'message':message,'imgurl':imgurl}, function responseCallback(responseData) {});
})
}
}
document.getElementById('close').onclick=function(){ if(ua==1){ //安卓回调 window.WebViewJavascriptBridge.callHandler('nativeClose', {'param':''}, function(responseData) {});
}else{ //ios回调
setupWebViewJavascriptBridge(function(bridge){bridge.callHandler('close', {'param':''}, function responseCallback(responseData) {});
})
}
}
iOS原生调用
#import "WebViewJavascriptBridge.h"
#import "UserInfo+CoreDataClass.h"
#import "YQViewController+YQShareMethod.h"
@interface SharedViewController ()
@property (nonatomic, strong) WebViewJavascriptBridge *bridge;
@property (nonatomic, strong)UIWebView *webView;
@property (nonatomic, strong) NSString *shareURL;
@property (nonatomic, strong) NSString *shareTitle;
@property (nonatomic, strong) NSString *shareMessage;
@property (nonatomic, strong) NSString *shareImgurl;
@end
@implementation SharedViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
[self prepareViews];
}
- (void)prepareViews {
self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];
self.webView.delegate = self;
[self.view addSubview:self.webView];
// NSString *filePath = [[NSBundle mainBundle] pathForResource:@"newRecrui" ofType:@"html"];
// NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
// self.urlStr = [NSString stringWithFormat:@"%@%@%@",H5BaseURL,EZNewRecrui,[UserEntity getUid]];
// NSURL *url = [[NSURL alloc] initWithString:filePath];
// [self.webView loadHTMLString:htmlString baseURL:url];
NSString *urlStr = [NSStringstringWithFormat:@"%@%@%@",H5BaseURL,EZNewRecrui,[UserEntity getUid]];
NSURL *url = [NSURL URLWithString:urlStr];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
// 3.开启日志 [WebViewJavascriptBridge enableLogging];
// 4.给webView建立JS和OC的沟通桥梁 self.bridge = [WebViewJavascriptBridgebridgeForWebView:self.webView]; [self.bridge setWebViewDelegate:self];
/* JS调用OC的API:打开分享 */ [self.bridge registerHandler:@"share" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"分享的url%@", data[@"url"]);
NSLog(@"分享的title%@", data[@"title"]);
NSLog(@"分享的message%@", data[@"message"]);
NSLog(@"分享的imgurl%@", data[@"imgurl"]);
NSLog(@"此处添加分享功能");
self.shareURL = data[@"url"];
self.shareTitle = data[@"title"];
self.shareMessage = data[@"message"];
self.shareImgurl = data[@"imgurl"];
[self shareView];
}]; [self.bridge registerHandler:@"close" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"点击返回了");
[self.navigationController popViewControllerAnimated:YES];
}];
}
- (NSMutableDictionary *)getShateParameters{
// YQGroupTableItem *item = [self.tableGroups objectAtIndex:curTableIndex];
// YQDiscoverFrame *frame = item.tableViewArray[curIndexPath.row]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
NSString *title = self.shareImgurl;
NSString *image = self.shareImgurl;
NSString *urlStr = self.shareURL;
NSURL *url = [NSURL URLWithString:urlStr];
// 通用参数设置
[parameters SSDKSetupShareParamsByText:title images:image url:url title:self.shareMessage type:SSDKContentTypeAuto];
return parameters;
}
- (void)viewWillAppear:(BOOL)animated {
// [SVProgressHUD show];
}
-(void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// [SVProgressHUD dismiss];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError*)error {
// [SVProgressHUD dismiss];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"---------%@", request.URL.absoluteString); self.hidesBottomBarWhenPushed = YES; NSString *urlString = request.URL.absoluteString; if ([urlString containsString:@"weixin://"]) { [[UIApplication sharedApplication]openURL:request.URL options:@{}completionHandler:^(BOOL success) { NSLog(@"=========%d", success);
}];
}
return YES;
}