小工具知识网

当前位置: 首页 > 行业知识 > ios 使用window.location.href 不能跳转微信短链处理过程以及解决方法

行业知识

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

2026-11-02 22:31:48 来源:互联网转载

需求背景:

        由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&app&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 微信的短链     及可正常跳转

上一篇:怎么查询mc的服务器号?

下一篇:怎么通过网页制作赚钱?