首页 » IOS » iOS与vue交互

iOS与vue交互

原文 http://blog.csdn.net/lvlemo/article/details/78738345

2017-12-08 02:01:02阅读(574)

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.iOSvue交互

#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>

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹