oc 与 js交互之vue.js
- 、vue.js 调用oc的方法并传值
vue.js 组件中调用方法:
//
// HDSpecialViewController.h
// headhunter
//
// Created by peter.zhang on 2017/11/20.
// Copyright © 2017年 HunterOn. All rights reserved.
//
#import "WebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface HDSpecialView:UIView <UIWebViewDelegate>
@property (nonatomic,strong) UIViewController * viewController;
//初始化
- (instancetype)initWithFrame:(CGRect)frame withViewController:(UIViewController *)viewController;
//清空缓存
- (void)clearCacheAndCookie;
@end
@protocol specialJavaScriptDelegate <JSExport>
-(void)goToSpecial:(NSString *)specialId type:(NSString *)type;
@end
@interface SpecialJsModel : NSObject <specialJavaScriptDelegate>
@property (nonatomic, weak) JSContext *jsContext;
@property (nonatomic, weak) UIWebView *webView;
@property (nonatomic,strong) UIViewController *vc;
@end
.m文件
//
// HDSpecialViewController.m
// headhunter
//
// Created by peter.zhang on 2017/11/20.
// Copyright © 2017年 HunterOn. All rights reserved.
//
#import "HDSpecialView.h"
#import "HDHotSearchViewController.h"
@implementation SpecialJsModel
//JS调用此方法进入高端专场
-(void)goToSpecial:(NSString *)specialId type:(NSString *)type
{
dispatch_async(dispatch_get_main_queue(), ^{
if (specialId&&![specialId isEqualToString:@""]) {
HDHotSearchViewController * vc = [[HDHotSearchViewController alloc]init];
Adver *adver = [[Adver alloc]init];
adver.pictureId = [specialId longLongValue];
adver.type = [type longLongValue];
vc.adver = adver;
[self.vc.navigationController pushViewController:vc animated:YES];
}
});
}
@end
@interface HDSpecialView ()
@property (strong, nonatomic) UIWebView *webView;
@property (nonatomic, strong) JSContext *jsContext;
@property (strong, nonatomic) NSString *urlString;
@end
@implementation HDSpecialView
- (instancetype)initWithFrame:(CGRect)frame withViewController:(UIViewController *)viewController{
self = [super initWithFrame:frame];
if (self) {
self.viewController = viewController;
self.backgroundColor =[UIColor whiteColor];
self.jsContext = [[JSContext alloc] init];
[self initWebView];
}
return self;
}
-(void)clearCacheAndCookie
{
[self cleanCacheAndCookie];
}
-(void)initWebView
{
NSString *str=nil;
#ifdef __DEBUG
//测试环境
str=@"https://xxxxxxxxxxxxx/special.html#/";
#else
//正式环境
str=@"https://xxxxxxxxxxxxx/special.html#/";
#endif
UIWebView *myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0,kScreen_Width,kScreen_Height-kHeight_NavBar - kHeight_TabBar)];
myWebView.delegate =self;
NSURL *url=[NSURL URLWithString:str];
self.webView=myWebView;
[myWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:120]];
myWebView.backgroundColor = kColor_BackGround;
[self addSubview:myWebView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
SpecialJsModel *model = [[SpecialJsModel alloc] init];
self.jsContext[@"mobileObject"] = model;
model.jsContext = self.jsContext;
model.webView = self.webView;
model.vc = self.viewController;
self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"异常信息:%@", exceptionValue);
};
[MBProgressHUD hideHUDInView:self.viewController.view];
//去除长按后出现的文本选取框
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
_urlString=request.URL.absoluteString;
return YES;
}
-(void)setUrlString:(NSString *)urlString
{
_urlString =urlString;
}
/**清除缓存和cookie*/
- (void)cleanCacheAndCookie
{
[self.webView stringByEvaluatingJavaScriptFromString:@"localStorage.clear();"];
//清除cookies
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
//清除UIWebView的缓存
NSURLCache * cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
[cache setDiskCapacity:0];
[cache setMemoryCapacity:0];
}
@end
二、oc调用vue.js 组件的方法
在webViewDidFinishLoad 代理方法中调用,因为这个时候vue的所有的组件节点都已经渲染
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//该方法是vue中组件的方法
[self call];
}
- (void)call{
// 之后在回调js的方法Callback把内容传出去
JSValue *Callback = self.jsContext[@"mobileChangeContent"];
//传值给web端
[Callback callWithArguments:@[@"4"]];
}
vue中的实现mobileChangeContent(参数)的方法
1、注意我们都知道调用vue的方法的时候要挂载到window上的方法
随意在组件中需要特殊处理,让组件的方法挂载到window的mobileChangeContent方法上
组件A
oc 与 js交互之vue.js的更多相关文章
- Vue.js 2 vs Vue.js 3的实现 – 云栖社区
Vue.js 2 vs Vue.js 3的实现 – 云栖社区 vue.js核心团队已经讨论过将在Vue3实现的变化.虽然API不会改变,但是数据响应机制(译者注:对数据改变的监听和通知)发生了变化.这 ...
- 分享Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站
这是个什么的项目? 使用 Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站. 博客线上地址:www.boblog.com Github地址:https: ...
- Js 框架之Vue .JS学习记录 ① 与Vue 初识
目录 与 Vue.js 认识 VUE.JS是个啥?有啥好处? Vue 的目标 战前准备 VUE.JS环境 VUE.JS 尝试一下,你就明白 第一步 实例化VUE 对象 第二步VueAPP 调用数据 ...
- 我们为什么以及是如何从 Angular.js 迁移到 Vue.js?
在我写这篇文章的时候,我们刚刚从我们的应用程序代码库中删除了最后一行AngularJS代码,结束了一个为期4个月的非侵入性工作,将我们的应用程序从AngularJS迁移到VueJS.在这篇文章中,我将 ...
- vue.js devtools-------调试vue.js的开发者插件
vue.js devtools插件: 作用: 以往我们在进行测试代码的时候,直接在console进行查看,其实这个插件雷同于控制台,只不过在vue里面,将需要查看的数据存放在一个变量里面了~ 效果图: ...
- JS框架_(Vue.js)带有星期日期的数字时钟
百度云盘 传送门 密码:tv1v 数字时钟效果: <!doctype html> <html> <head> <meta charset="utf- ...
- Windows使用Node.js自动生成Vue.js模版环境部署步骤-----记录
node.js官网下载并安装node 进入node文档目录下,运行cmd 输入 node -v 查看node版本 出现表示安装完成 输入 npm -v 显示npm版本信息 安装cnpm 输入 npm ...
- Vue.js 和 MVVM 小细节
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...
- MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录
注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...
随机推荐
- the c programing language 学习过程4
4Functions and Program Structure scratch 刮擦 starting over from scratch从头开始 reside驻留 separately 分别的 f ...
- 洛谷P3381 - 【模板】最小费用最大流
原题链接 题意简述 模板题啦~ 题解 每次都以费用作为边权求一下最短路,然后沿着最短路增广. Code //[模板]最小费用最大流 #include <cstdio> #include & ...
- java调用shell脚本,并获得结果集的例子
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- jqeury显示前几个,隐藏后几个,点击后隐藏前几个显示后几个
<script type="text/javascript"> $(".ul li").each(function(){ if($(this).in ...
- mysql事务介绍
什么是事务 一组sql语句操作单元组内所有sql完成一个业务如果整组成功,意味着组内的全部的sql成功如果其中任何一个失败,意味着整个操作失败 数据回到操作前的状态 事务的特点 1.多条sql语句组成 ...
- CAN总线基础知识(一)
1.CAN总线是什么? CAN(Controller Area Network)是ISO国际标准化的串行通信协议.广泛应用于汽车.船舶等.具有已经被大家认可的高性能和可靠性. CAN控制器通过组成总线 ...
- 芝麻HTTP:代理的基本原理
我们在做爬虫的过程中经常会遇到这样的情况,最初爬虫正常运行,正常抓取数据,一切看起来都是那么美好,然而一杯茶的功夫可能就会出现错误,比如403 Forbidden,这时候打开网页一看,可能会看到&qu ...
- 配置文件properties读取使用的好方法
首先在spring配置文件applicationContext.xml中配置. <bean id="placeholderConfig" class="com.be ...
- filter()和find()的区别
<div class="css"> <p class="rain">测试1</p> </div> <div ...
- as版本切换导致项目损坏,无法启动。环境依赖混乱
以下方法都不管用, 新建一个项目setting的 把main拷过去就行了!!!! Error:(5, 0) Gradle DSL method not found: 'google()'Possibl ...