ios 使用window.location.href 不能跳转微信短链处理过程以及解决方法

本文讨论了在H5页面通过微信API生成的短链在iOS上无法正常跳转,通过分析发现是window.location.href在iOS中的iframe限制。解决方案是使用window.top.location.href实现跨框架跳转。
摘要由CSDN通过智能技术生成

需求背景:

        由h5提供页面,通过后台请求微信api生成对应的schemal短链,该h5页面嵌入到原生的ios以及安卓app上,当用户点击后通过短连接跳转到其他小程序中

以下为生成微信scheme代码示例,生成后短链为:weixin://dl/business/?t=l97aMn7aZF 这种

 /**
     *  生成短连接
     * @param data
     * @return
     */
    public static String getUrlScheme(JSONObject data) {
        try {
            log.info("==>获取小程序跳转地址,mchnt:{}", data);
            String appId = data.getString("appId");
            String secret = data.getString("secret");
            String accessToken = getAccessToken(appId, secret);
            return generateWechatScheme(accessToken, data);
        } catch (Exception e) {
            log.error("==>获取小程序跳转地址失败", e);
            throw new FuiouException("5022","获取小程序跳转地址失败");

        }
    }

    /**
     * 向微信服务端请求生成小程序 scheme
     */
    public static String generateWechatScheme(String accessToken, JSONObject data) {
        CloseableHttpClient client = HttpClientUtil.createClient();
        String url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" + accessToken;
        HttpPost request = new HttpPost(url);
        request.setHeader("Content-Type", "application/json; charset=utf-8");

        String path = data.get("path").toString();
        String queryStr = data.get("queryStr").toString();
        String env_version = data.get("env_version").toString();

        // 构建请求体 JSON
        String requestBody = "{\"jump_wxa\":{\"path\":\"" + path + "\",\"query\":\"" + queryStr
                + "\",\"env_version\":\"" + env_version
                + "\"},\"is_expire\":false,\"expire_type\":1,\"expire_interval\":" + 1 + "}";

        request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
        log.info("==>调用微信generatescheme接口,request body:" + requestBody);
        String response = HttpClientUtil.executeAndClose(client, request);
        log.info("==>调用微信generatescheme接口,response:" + response);
        JSONObject jsResp = JSONObject.parseObject(response);
        String openlink = jsResp.getString("openlink");
        log.info("==>获取到的微信小程序scheme:" + openlink);
        return openlink;
    }


    /**
     * 获取 access_token
     */
    private static String getAccessToken(String appId, String secret) {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
        CloseableHttpClient client = HttpClientUtil.createClient();
        HttpGet request = new HttpGet(url);
        String response = HttpClientUtil.executeAndClose(client, request);
        log.info("==>获取微信accessToken,response:" + response);
        JSONObject jsResp = JSONObject.parseObject(response);
        String accessToken = jsResp.getString("access_token");
        return accessToken;
    }

所遇到问题:

        短链如:weixin://dl/business/?t=l97aMn7aZF

        通过短链在前端利用js  window.location.href = weixin://dl/business/?t=l97aMn7aZF 进行跳转,在安卓上面能正常跳转,但是ios无法跳转,且没有反应

解决问题思路

        1、检查schema 是否正确,在ios真机测试也是可以跳;排除

        2、将window.location.href 替换为 window.location  以及换为 windos.open(xx) 等多种方式均不可行;

       3、怀疑是h5页面的meta 写的有问题,随即替换为meta 信息,进行测试,还是不行;

       4、本来都准备放弃,让ios提供一个原生方法,自己这边判断如果是ios就调用原生方法进行跳转,附一段js ios 判断代码

function isIos(){
    var u = window.navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    return     isiOS;
}

       5、这时候经同事提醒,将链接放到其他页面,看是否可跳转,然后将链接放到首页测试是否可以跳转,这时候神奇的事情发生了,竟然ios 能正常跳了,同样是 window.location.href 

     6、拿到h5页面开始分析首页和2级页面的区别,发现首页没有无层级关系,而二级目录有一个ifream页面,以下为二级页面

    

最终解决方案

        经过以上分析后得知,window.location.href 不能跳出父框架,因为需要唤起小程序,所以需要跳出ifream ,但是安卓没问题,可能是语法的限制,最终知道解决方案

window.top.location.href = xx 微信的短链     及可正常跳转
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值