美文网首页
web页面打开iOS应用

web页面打开iOS应用

作者: 觞咏畅情 | 来源:发表于2019-07-24 11:31 被阅读0次

    如题,这种场景现在已经很普遍了,处理的方式一般就是两种
    1.使用URL Scheme;
    2.iOS9出来的universal link(通用链接)。
    当然在使用的过程各有不同,接下来就简单总结一下:

    1、URL Scheme
    $(".button").on("click",function(){
        if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i))
        {
            var loadDateTime = new Date().getTime();
            window.setTimeout(function() {
                var timeOutDateTime = new Date().getTime();
                if (timeOutDateTime - loadDateTime < 3000) {
                    window.location.href = "https://xxx.com/down.html";//下载链接
                } else {
                    window.close();
                }
            },1500);
            window.location.href = "demo://";//当前ios应用的scheme协议
        }
        if(navigator.userAgent.match(/android/i))
        {
            var loadDateTime = new Date().getTime();
            window.setTimeout(function() {
                var timeOutDateTime = new Date().getTime();
                if (timeOutDateTime - loadDateTime < 3000) {
                    window.location.href = "https://xxx.com/down.html";//下载链接
                } else {
                    window.close();
                }
            },1500);
            window.location.href = "demo://";//当前Android应用的协议
        }
    });
    

    这种方式在web页面点击的时候会有系统弹窗提示,而且微信和QQ内部打开并不会提示,需要前端页面添加提示在系统浏览器中去打开,这种方式还有一个缺点是用定时器去跳转,很多时候会在你点击了打开app后还是会跳转到下载页面,体验较差。

    2、universal link(通用链接)

    IOS9.0以上开始支持通用链接,而且苹果官方也推荐用此方式官方链接
    使用方式可以分为这几步:
    1:在开发者平台将应用对应的appID支持Associated Domains

    1.jpg
    2:创建apple-app-site-association文件:
    apple-app-site-association是iOS中一个JSON格式的“通用链接”配置文件,在其paths键中设置通用链接的具体规则
    {
        "applinks": {
            "apps": [],
            "details": [
                {
                    "appID": "9JA89QQLNQ.com.apple.wwdc",
                    "paths": [ "/wwdc/news/", "/videos/wwdc/2015/*"]
                },
                {
                    "appID": "这里是teamID+Bundle ID的组合",
                    "paths": [ "*" ]
                }
            ]
        }
    }
    

    appID的值为这里是teamID+Bundle ID的组合
    paths这个是用来控制可识别链接的,星号的写法代表了可识别域名下所有链接。
    注意:apple-app-site-association文件不能带后缀,最好放到域名对应的根目录下保证https://<domain>/apple-app-site-association可以正常访问去下载


    2.jpg 3:打开xcode配置项目Capabilities->Associated Domains中添加对应的applinkes,注意这里必须要以applinkes:开头,后面为服务器域名地址,该地址就是apple-app-site-association文件存放的地址: 3.png

    4.重新运行app后或者重新安装,然后可以在浏览器中打开对应的页面去点击按钮尝试跳转打开app。
    前段web页面可以修改

    if(isiOS9()){//判断是否为ios9以上
      //当前iOS通用连接地址,后面携带的参数
      location.href = "https://www.xxxx.com?type=1&id=1"
      return;
    }
    window.location.href = "demo://";
    

    苹果有个地址可以测试上面域名对应的文件这里,注意如果提示

    Error no apps with domain entitlements
    The entitlement data used to verify deep link dual authentication is from the current released version of your app. This data may take 48 hours to update.

    也可以正常使用

    5.如果有参数需要跳转可以实现appdelegate的代理方法

    -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
        if([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]){
            NSURL *webUrl = userActivity.webpageURL;
            if([webUrl.host isEqualToString:@"www.xxxx.com"]){//后续可以按指定路径去跳转
                
            }else{
                [[UIApplication sharedApplication] openURL:webUrl];
            }
        }
        return YES;
    }
    

    相关文章

      网友评论

          本文标题:web页面打开iOS应用

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