iOS与vue交互(点击网页传值到OC)

25 篇文章 0 订阅

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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值