美文网首页
iOS 网页进入APP

iOS 网页进入APP

作者: 幻想无极 | 来源:发表于2019-03-04 17:07 被阅读20次

    前言

    使用scheme跳转,URL参数分离,跳转路由

    scheme

    转送门
    https://www.jianshu.com/p/138b44833cda
    https://www.jianshu.com/p/0811ccd6a65d

    safari

    safari地址MouoApp://shopDetal?userId=10965

    使用

    AppDelegate

    // iOS 9 以上请用这个
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options{
     if([url.scheme isEqualToString:@"mouoapp"]){
                //url.query
                //进入的时候判断逻辑为
                if (self.mainController && [MouoUserManager sharedAccountManager].user) {
                    [JumpManager gotoControllerWithScheme:url.absoluteString controller:self.mainController.currentSelectController];
                }
                return YES;
            }
    }
    

    路由类

    + (MouoBaseController *)gotoControllerWithScheme:(NSString *)scheme controller:(MouoBaseController *)controller {
        //商家详情进入
        if ([scheme containsString:@"shopDetal"]) {
            NSLog(@"scheme地址:%@",scheme);
            NSString *userId = [self getParamByName:@"userId" URLString:scheme];
            return [self gotoShopController:@{@"Id":userId} controller:controller];
        }
        return nil;
    }
    

    url参数分离

    + (NSString *)getParamByName:(NSString *)name URLString:(NSString *)url
    {
        NSError *error;
        NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", name];
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
        
        // 执行匹配的过程
        NSArray *matches = [regex matchesInString:url
                                          options:0
                                            range:NSMakeRange(0, [url length])];
        for (NSTextCheckingResult *match in matches) {
            NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
            return tagValue;
        }
        return @"";
    }
    

    html源码

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name=“viewport” content=“initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width=device-width”   />
    
            <title></title>
        </head>
        <body>
            
            <a id="openApp">陌友App进入</a>
    
            <script>
                var ios_schema = "MouoApp://shopDetal?userId=10965",
                    ios_download_url = "https://itunes.apple.com/cn/app/%E9%99%8C%E5%8F%8B-%E9%99%8C%E7%94%9F%E4%BA%BA%E4%BA%A4%E5%8F%8B/id1210615145?mt=8";
                var android_schema = "唤醒协议",
                    android_download_url = "";
                document.getElementById("openApp").onclick = function(e) {
                    if (/(Android)/i.test(navigator.userAgent)) {
    
                        var ifr = document.createElement("iframe");
                        ifr.style.display = "none";
                        ifr.src = android_schema;
                        document.body.appendChild(ifr);
                        window.setTimeout(function() {
                            document.body.removeChild(ifr);
                            //                      if(confirm("检测到您手机可能没有安装app,您要前往下载君凯商联网app吗?")) {
                            location.href = android_download_url; //打开app下载地址,有app同事提供
                            //                      }
                        }, 2000)
    
                    }
    
                    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    
                        var install = false;
                        var loadTime = new Date();
                        location.href = ios_schema;
                        setTimeout(function() {
                            var outTime = new Date()
                            if (confirm("检测到您手机可能没有安装app,马上去苹果商店下载吧~ ")) {
                                location.href = ios_download_url;
                            }
                        }, 2000)
                    }
    
                }
            </script>
    
        </body>
    
    </html>
    
    

    相关文章

      网友评论

          本文标题:iOS 网页进入APP

          本文链接:https://www.haomeiwen.com/subject/moiiuqtx.html