#import "AboutMyViewController.h"
#import "HtmlWKWebView.h"
@interface AboutMyViewController ()<WKScriptMessageHandler>
//分享
@property (nonatomic,weak)UIView *bageView;
@property (nonatomic,weak)UIView *contentView;
@property WebViewJavascriptBridge *bridge;
@property(nonatomic,strong)HtmlWKWebView * webView;
@end
@implementation AboutMyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
[self creatNav];
//创建网页
[self createWebView];
}
-(void)creatNav{
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(pressButtonLeft)];
self.navigationItem.leftBarButtonItem = item;
//设置中间标题
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(100, 0, self.view.frame.size.width-200, 64);
label.text = @"嗅美";
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:18];
self.navigationItem.titleView = label;
}
-(void)pressButtonLeft{
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)createWebView{
//进行配置控制器
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
//实例化对象
configuration.userContentController = [WKUserContentController new];
//与JS交互的注册,JS调用OC方法
[configuration.userContentController addScriptMessageHandler:self name:@"appShareIOS"];
//初始化
_webView = [[HtmlWKWebView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) configuration:configuration];
//yuH5有交互的初始化完手动调用创建UI
_webView.actionJS = @"和JS有交互";
//传wkWebview过去注册KVO开启进度条
self.webView.wkWebview = _webView;
//关闭反弹
self.webView.scrollView.bounces = NO;
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",AboutXiumeiNew,GET_VERSION]]]];
[self.view addSubview:_webView];
}
/**
* JS给原生传数据:JS调用原生的方法
*/
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
//得到JS返回的数据
// NSDictionary *dict = message.body;
NSLog(@"----WKwebView交互----");
//分享
[self shareXiumei];
}
//分享
-(void)shareXiumei{
//分享
UIImage * image = [UIImage imageNamed:@"嗅美logo"];
[ShareView share:self ShareTitle:@"嗅美 超in美妆护肤穿衣搭配助手-看美容美妆心得分享,享正品折扣免费试用" WithContent: [NSString stringWithFormat:@"版本:%@",GET_VERSION] ShareUrl:@"http://www.xiumeiapp.com/androidToH5/shareApp.html" shareImage:image ReporStrType:nil shareType:nil completion:^(NSString *code) {
}];
}
//注意!注意!注意:上面将当前ViewController设置为MessageHandler之后需要在当前ViewController销毁前将其移除,否则会造成内存泄漏。
-(void)dealloc{
[_webView.configuration.userContentController removeScriptMessageHandlerForName:@"appShareIOS"];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.tabBarController.tabBar.hidden = YES;
//友盟统计
[MobClick beginLogPageView:[NSString stringWithFormat:@"%@", self.class]];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//友盟统计
[MobClick endLogPageView:[NSString stringWithFormat:@"%@", self.class]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
WKWebView与JS交互代码(OC)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 简单说两句,混合开发的App(Hybrid App)就是在一个App中内嵌一个轻量级的浏览器,一部分原生的功能改为...
- 参考博客:iOS中UIWebView与WKWebView、JavaScript与OC交互、Cookie管理看我就够...
- 一.WKWebView的使用:参考:http://www.360doc.com/content/15/1111/1...
- 简述 如今很多复杂的页面,为了进度,选择了H5页面开发,所以就必须在oc中嵌套使用H5页面,这样更快的促进项目。但...