iOS与vue交互(点击网页传值到OC)
1.初始化UIWebView- (void)viewDidLoad {[super viewDidLoad];UIWebView*webV=[[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];_webView=webV;webV.backgroundColor=[UICo...
·
1.初始化UIWebView
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView*webV=[[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView=webV;
webV.backgroundColor=[UIColor groupTableViewBackgroundColor];
webV.delegate=self;
NSString * urlString = @"";
#ifdef DEBUG
urlString = @"http://ad189dff-3e44-4797-93c8-6c739bc480ed.coding.io/dist/index.html#/category";
#else
urlString = @"http://dev.huaweidun.com:35/EnjoyTheService/Index";
#endif
[webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
2.iOS与Vue交互
#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//1.传递accesstoken
LoginModel * login = [LoginMethods getLoginModel];
NSString *jsStr = [NSString stringWithFormat:@"window.localStorage.setItem('hwdtoken','%@')",login.accessToken];
[webView stringByEvaluatingJavaScriptFromString:jsStr];
//2.iOS监听vue的函数
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//定义好JS要调用的方法, share就是调用的share方法名
context[@"share"] = ^() {
NSLog(@"+++++++Begin Log+++++++");
NSArray *args = [JSContext currentArguments];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"方式二" message:@"这是OC原生的弹出窗" delegate:self cancelButtonTitle:@"收到" otherButtonTitles:nil];
[alertView show];
});
for (JSValue *jsVal in args) {
NSLog(@"%@", jsVal.toString);
}
NSLog(@"-------End Log-------");
};
}
2.1.category.vue
<template>
<div class="category_box">
分类
<button @click="clickDidClick">点击我</button>
{{token}}
{{signed}}
</div>
</template>
<script>
export default{
data:function () {
return {
token:''
}
},
methods:{
clickDidClick:function () {
//iOS可以监听到这个方法
share('11','22','33')
}
},
created:function () {
},
computed: {
// 这里!判断localStorage的变化
signed () {
if (localStorage.getItem('hwdtoken') != null) {
this.token=window.localStorage.getItem('hwdtoken');
return true
}
return false
}
},
}
</script>
更多推荐
已为社区贡献8条内容
所有评论(0)