我是靠谱客的博主 糟糕裙子,最近开发中收集的这篇文章主要介绍iOS原生与html交互 使用第三方WebViewJavascriptBridge,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HTML页面代码

<!DOCTYPE html>
<html xmlns:http="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>迎新好礼</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
</head>
<link rel="stylesheet" type="text/css" href="http://at.alicdn.com/t/font_621453_gghsyu58tj7cik9.css">
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div class="container">
    <div class="back" id="close"><i class="allure allure-fanhui"></i><p class="m-tit">迎新享好礼</p>
      <p id="close"></p>
    </div>
    <div class="title" style="height: 44px;line-height: 44px;"><p>迎新享好礼</p></div>
    <div class="desc">
     <p class="line">每邀请一个E招新用户发布有效简历</p>
     <p class="line big">双方最低可赚 5元,累计无上限</p>
     <p class="line">活动时间5.1-6.1</p>
   </div>
   <div class="share">
     <div class="content">
      <div class="line"></div>
      <div class="tit">赚钱方式</div>
      <div class="btn">
       <i class="allure allure-fenxiang"></i>
       <p id="share">点此分享赚钱</p>
     </div>
   </div>
 </div>
 <div class="result">
   <div class="content">
    <div class="block">
     <div class="tit">奖励金(元)</div>
     <div class="money">0</div>
     <div class="unit">元</div>
   </div>
   <div class="block">
     <div class="tit">已邀请(人)</div>
     <div class="money">0</div>
     <div class="unit">人</div>
   </div>
   <div class="stub">
     <div class="circle top"></div>
     <p>已获得</p>
     <div class="circle bottom"></div>
   </div>
 </div>
</div>
<div class="more">
 <div class="content">
  <p class="tit">如何操作?想赚更多?戳!</p>
  <p class="tit-d">赚钱攻略</p>
  <div class="btn"><p><a href="rule.html">GO</a></p></div>
</div>
</div>
</div>
</body>
<script>
  var ua=0;
  window.οnlοad=function () {
    var u = navigator.userAgent, app = navigator.appVersion;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
    var isIOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    ua=isAndroid?1:2;
  }
    // 这段代码是固定的,必须要放到js中
  function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
  }
  document.getElementById('share').οnclick=function(url,imgurl,title,message){
    console.log(url,imgurl,title,message)
    if(ua==1){
      //安卓回调
      window.WebViewJavascriptBridge.callHandler('nativeShare', {'url':url,'title':title,'message':message,'imgurl':imgurl}, function(responseData) {});
    }else{
      //ios回调
      setupWebViewJavascriptBridge(function(bridge){
        bridge.callHandler('share', {'url':url,'title':title,'message':message,'imgurl':imgurl}, function responseCallback(responseData) {});
      })
    }
  }
  document.getElementById('close').οnclick=function(){
    if(ua==1){
     //安卓回调
     window.WebViewJavascriptBridge.callHandler('nativeClose', {'param':''}, function(responseData) {});
    }else{
      //ios回调
      setupWebViewJavascriptBridge(function(bridge){bridge.callHandler('close', {'param':''}, function responseCallback(responseData) {});})
    }
  }
</script>

</html>

iOS原生代码 

#import "WebViewJavascriptBridge.h"

#import "UserInfo+CoreDataClass.h"

#import "YQViewController+YQShareMethod.h"


@interface SharedViewController ()<UIWebViewDelegate>


@property (nonatomic, strong) WebViewJavascriptBridge *bridge;


@property (nonatomic, strong)UIWebView *webView;


@property (nonatomic, strong) NSString   *shareURL;

@property (nonatomic, strong) NSString   *shareTitle;

@property (nonatomic, strong) NSString   *shareMessage;

@property (nonatomic, strong) NSString   *shareImgurl;


@end


@implementation SharedViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    [self prepareViews];

}


- (void)prepareViews {

    self.webView = [[UIWebView alloc]initWithFrame:self.view.frame];

    self.webView.delegate = self;

    [self.view addSubview:self.webView];

    

//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"newRecrui" ofType:@"html"];

//    NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    

//    self.urlStr = [NSString stringWithFormat:@"%@%@%@",H5BaseURL,EZNewRecrui,[UserEntity getUid]];

//    NSURL *url = [[NSURL alloc] initWithString:filePath];

//    [self.webView loadHTMLString:htmlString baseURL:url];

    

    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@",H5BaseURL,EZNewRecrui,[UserEntity getUid]];

    NSURL *url = [NSURL URLWithString:urlStr];

    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];

    // 3.开启日志

    [WebViewJavascriptBridge enableLogging];

    

    // 4.给webView建立JS和OC的沟通桥梁

    self.bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView];

    [self.bridge setWebViewDelegate:self];

    

    

    /* JS调用OC的API:打开分享 */

    [self.bridge registerHandler:@"share" handler:^(id data, WVJBResponseCallback responseCallback) {

        NSLog(@"分享的url%@", data[@"url"]);

        NSLog(@"分享的title%@", data[@"title"]);

        NSLog(@"分享的message%@", data[@"message"]);

        NSLog(@"分享的imgurl%@", data[@"imgurl"]);

        NSLog(@"此处添加分享功能");

        self.shareURL = data[@"url"];

        self.shareTitle = data[@"title"];

        self.shareMessage = data[@"message"];

        self.shareImgurl = data[@"imgurl"];

        

        [self shareView];

    }];

    

    [self.bridge registerHandler:@"close" handler:^(id data, WVJBResponseCallback responseCallback) {

        NSLog(@"点击返回了");

        [self.navigationController popViewControllerAnimated:YES];

    }];

    

    

}


- (NSMutableDictionary *)getShateParameters

{

    //    YQGroupTableItem *item = [self.tableGroups objectAtIndex:curTableIndex];

    //    YQDiscoverFrame *frame = item.tableViewArray[curIndexPath.row];

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

    NSString *title = self.shareImgurl;

    

    NSString *image = self.shareImgurl;

    

    NSString *urlStr = self.shareURL;

    NSURL *url = [NSURL URLWithString:urlStr];

    // 通用参数设置

    

    [parameters SSDKSetupShareParamsByText:title

                                    images:image

                                       url:url

                                     title:self.shareMessage

                                      type:SSDKContentTypeAuto];

    return parameters;

}





- (void)viewWillAppear:(BOOL)animated {

    //    [SVProgressHUD show];

}

-(void)webViewDidStartLoad:(UIWebView *)webView {

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    //    [SVProgressHUD dismiss];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    //    [SVProgressHUD dismiss];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    NSLog(@"---------%@", request.URL.absoluteString);

    self.hidesBottomBarWhenPushed = YES;

    NSString *urlString = request.URL.absoluteString;

    

    if ([urlString containsString:@"weixin://"]) {

        [[UIApplication sharedApplication]openURL:request.URL options:@{} completionHandler:^(BOOL success) {

            NSLog(@"=========%d", success);

        }];

    }

    return YES;

}



最后

以上就是糟糕裙子为你收集整理的iOS原生与html交互 使用第三方WebViewJavascriptBridge的全部内容,希望文章能够帮你解决iOS原生与html交互 使用第三方WebViewJavascriptBridge所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部