陈斌彬的技术博客

Stay foolish,stay hungry

Ios与web交互

web html文件加上scipt

 <script>
            (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
            function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
            e=o.createElement(i);r=o.getElementsByTagName(i)[0];
            e.src='https://www.google-analytics.com/analytics.js';
            r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
            ga('create','UA-XXXXX-X','auto');ga('send','pageview');
        </script>
        <script>

            function pushSubViewController () {
                setupWebViewJavascriptBridge(function(bridge) {
                    bridge.callHandler('push',
                        {
                            'detailID': '斌彬哥哥',
                            'actionName':'push'
                        },
                        function responseCallback(responseData) {
                            console.log("JS received response:", responseData)
                        })
                })
            }

        </script>
        <script>
            //        init JS  //固定写法
            window.onerror = function(err) {
                log('window.onerror: ' + err)
            }

            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)
            }
        </script>

            <p>3.跳转到某些子界面</p>
        <button id="push" onclick="pushSubViewController()">pushSubViewController</button>

ios端导入

img

导入

#import "WebViewJavascriptBridge.h"
#import "HybirdUrlHandler.h"
#import "HyBridBridge.h"

添加

@property WebViewJavascriptBridge *bridge;
@property (strong, nonatomic) HyBridBridge *hybridBridge;

使用

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/src/"]]]];

//http://localhost:8080/src/
//http://ionic-person-6e53.coding.io/
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView];
[self.hybridBridge registerHybridUrlHanlder:self andBridge:self.bridge];

回调

//懒加载
- (HyBridBridge *)hybridBridge {
    if (!_hybridBridge) {
        _hybridBridge = [[HyBridBridge alloc] init];
    }
    return _hybridBridge;
}

- (NSArray *)actionNames {  //定义可支持的方法名称
    return @[@"push"];
}

//HTML 部分 需要定义 方法名称
//传参的时候 带上方法名字  我这里写的是actionName
- (BOOL)handleDictionAry:(NSDictionary *)dictionary callback:(HybridCallbackBlock)callbackBlock {
    NSString *actionTag = dictionary[@"actionName"];

    if ([actionTag isEqualToString:@"push"]) {

        NSString *detailID = dictionary[@"detailID"];
        NSLog(@"detailID is %@",detailID);
        NSLog(@"push view controller");
        BBZhiFuViewController *vc = [[BBZhiFuViewController alloc]init];
        vc.detailID = detailID;
        [self.navigationController pushViewController:vc animated:YES];
        return YES;
    }
    return NO;
}