iOS开发 与H5进行交互

1. H5调用 iOS

调用

          window.location.href = 'TS://phone';

接收

- (BOOL)webView:(nonnull UIWebView *)webView shouldStartLoadWithRequest:(nonnull NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@"TS://"];
    NSUInteger location = range.location;
//    xmg://openCamera
    if (location != NSNotFound) {
        NSString *str = [url substringFromIndex:location + range.length];
        NSLog(@"%@", str);
        SEL sel = NSSelectorFromString(str);
        [self performSelector:sel withObject:nil];
    }
    
    return YES;
}

2. iOS调用 H5

  • 直接 load

  • 通过 JS


    NSString *str2 =@"document.getElementById('footer').remove();";
    [webView stringByEvaluatingJavaScriptFromString:str2];

3. 发消息


      window.webkit.messageHandlers.AppModel.postMessage({body: response});

4. 参数


    NSString *offerId = [NSString getIdFromUrlParams:abString andCode:@"offerId"];

5. 上一段代码


$(function(){
        $(".btn_share").click(function() {
            var is_app = "";

            //这里直接和android与ios交互
            if(is_app == 'android'){
                var r = "{\"body\":'shareAction'}";
                window.APPmai.get_id(r);
            }else if(is_app == 'ios'){
                window.webkit.messageHandlers.APPmodel.postMessage({body:'shareAction'});
            }else{
                alert("请用android或ios打开");
            }
        });
    })

6. 判断设备类型(JS)

设备类型判断的测试url


    function browserRedirect() {
        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
        document.writeln("您的浏览设备为:");
        if (  bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
            document.writeln("android 类似以及其他设备");
        }
        else  if (bIsIpad )
        {
            document.writeln("ipad");
        }
        else  if (bIsIphoneOs )
        {
            document.writeln("iphone");
        }
        else {
            document.writeln("pc");
        }
    }

    browserRedirect();

你可能感兴趣的:(iOS开发 与H5进行交互)