最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

解决cordova跳转本机自带浏览器的问题

运维笔记admin0浏览0评论

原文链接cordova启动首页,访问远程服务器web_app
幸福就是 你遇到一个问题千方百计找不到答案快疯了的时候,突然看到了希望。

由于已经在微信浏览器(或者手机浏览器)中开发好了web应用,现在通过app打开应用访问服务页面

第一种尝试 —— 将web应用的代码一起打包到web应用中,页面全部从app本地加载代码,但是,web请求的数据是远程,因此跨域了

第二种尝试 —— 本地webview直接访问远程地址,就类似于浏览器访问远程一样,这样就不存在跨域,代码如下:

Html代码

<!DOCTYPE html>  
<html>  
    <head>  

        <meta name="format-detection" content="telephone=no">  
        <meta name="msapplication-tap-highlight" content="no">  
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">  
        <title>Hello World</title>  
    </head>  
    <body>  
        <script type="text/javascript" src="cordova.js"></script>  
        <script type="text/javascript" src="js/index.js"></script>  
    </body>  
    <script>  
            window.location.href="http://www.sqhzg/house/index.html#/index";  
    </script>  
</html> 

备注:

默认index.html页面中

Html代码

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">  

应该删除掉,否则页面无法正常跳转。

修改config.xml配置文件,允许webview访问页面,否则app会拉动本地的浏览器显示内容。

Config.xml代码

<?xml version='1.0' encoding='utf-8'?>  
<widget id="com.jianan.wuye" version="0.0.1" xmlns="http://www.w3/ns/widgets" xmlns:cdv="http://cordova.apache/ns/1.0">  
    <preference name="loglevel" value="DEBUG" />  
    <feature name="Whitelist">  
        <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />  
        <param name="onload" value="true" />  
    </feature>  
    <allow-intent href="market:*" />  
    <name>wuye</name>  
    <description>  
        A sample Apache Cordova application that responds to the deviceready event.  
    </description>  
    <author email="dev@cordova.apache" href="http://cordova.io">  
        Apache Cordova Team  
    </author>  
    <content src="index.html" />  
    <access origin="*" />  
    <allow-intent href="http://*/*" />  
    <allow-intent href="https://*/*" />  
    <allow-intent href="tel:*" />  
    <allow-intent href="sms:*" />  
    <allow-intent href="mailto:*" />  
    <allow-intent href="geo:*" />  

 <allow-navigation href="http://*/*" />  
 <allow-navigation href="https://*/*" />  
 <allow-navigation href="data:*" />  

 <access origin="*" />  
 <access origin="http://*/*" />  
 <access origin="https://*/*" />  

</widget>  



转自“黄彪的学习笔记”,感谢!

发布评论

评论列表(0)

  1. 暂无评论