不允许加载本地资源ios9 cordova应用程序 [英] not allowed to load local resource ios9 cordova app

查看:1005
本文介绍了不允许加载本地资源ios9 cordova应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <! -  index.html中的CSP元标记 - >< head> < meta charset =utf-8> < meta http-equiv =X-UA-Compatiblecontent =IE = edge,chrome = 1> < meta name =viewportcontent =width = device-width,height = device-height,user-scalable = no> < meta http-equiv =Content-Security-Policycontent =default-src *; style-src'self''unsafe-inline''unsafe-eval'; script-src'self''unsafe-inline' 'unsafe-eval';> < title>测试应用< / title>< / head><! - 配置白名单插件的config.xml文件 - ><?xml version =1.0encoding =utf-8?& < widget xmlns =http://www.w3.org/ns/widgetsxmlns:gap =http://phonegap.com/ns/1.0id =com.abc.testAppversionCode =10 version =2.0.0> < name> TestApp< / name> < description>示例应用下载并启动混合应用< / description> < author email =Test@gmail.comhref =http://1ct.es>测试< / author> < preference name =orientationvalue =portrait/> < preference name =fullscreenvalue =true/> < preference name =webviewbouncevalue =false/> < icon src =icon.png/> < content src =index.html/> < gap:platform name =ios/> < gap:plugin name =cordova-plugin-whitelistsource =npm/> < gap:plugin name =org.apache.cordova.fileversion =1.3.3/> < gap:plugin name =org.apache.cordova.file-transferversion =0.5.0/> < gap:plugin name =org.apache.cordova.inappbrowserversion =0.5.2/> < gap:plugin name =org.chromium.zipversion =2.1.0/> < gap:config-file platform =iosparent =NSAppTransportSecurity> < dict> < key> NSAllowsArbitraryLoads< / key> < true /> < / dict> < / gap:config-file> < allow-navigation href =*/> < allow-intent href =*/> < access origin =*/> < / widget>  



这是代码段的一部分获取应用程序的本地路径并在iframe中加载路径:

  var appLocalUrl = cordova.file.dataDirectory + AppId +/index.html; 
iframe [0] .contentWindow.location.replace(appLocalUrl);

应用程序下载到本地路径
,但是当我们尝试在iframe结束时错误消息:


不允许加载本地资源:{file:/// var / mobile / Containers / Data / Application / B8BF6DE2-228D-46B9-A84D-D2122D152385 / Library / NoCloud / 970 / index.html}


有任何帮助吗?

解决方案

所以我已经努力了很多与此相关。
最新的iOS使用WKWebView。它似乎将本地文件视为来自远程服务器,即使它们在应用程序本身,并且此类请求被阻止。参考来源



以下解决方案适用于我:


  1. 添加Corodova文件插件。 p>

    cordova插件add cordova-plugin-file


  2. p>将本地文件路径更改为:



    cdvfile://localhost/bundle/www/you_folder_name/file_name.mp3



<!--CSP Meta Tags in index.html -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,height=device-height,user-scalable=no">
        <meta http-equiv="Content-Security-Policy"
            content="default-src *;
            style-src 'self' 'unsafe-inline' 'unsafe-eval';
            script-src 'self' 'unsafe-inline' 'unsafe-eval';">
    <title>Test App</title>
</head>

<!-- config.xml file with whitelist plugin -->
<?xml version="1.0" encoding="utf-8" ?>
<widget xmlns   = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id          = "com.abc.testApp"
    versionCode = "10"
    version     = "2.0.0" >
  <name>TestApp</name>
  <description>
    Sample app to download and launch hybrid app
  </description>
  <author email="Test@gmail.com" href="http://1ct.es">
    Test
  </author>

  <preference name="orientation" value="portrait" />
  <preference name="fullscreen"  value="true" />
  <preference name="webviewbounce" value="false" />

  <icon src="icon.png" />

  <content src="index.html" />
  
  <gap:platform name="ios" />
  
  <gap:plugin name="cordova-plugin-whitelist" source="npm" />
  <gap:plugin name="org.apache.cordova.file" version="1.3.3" />
  <gap:plugin name="org.apache.cordova.file-transfer" version="0.5.0" />
  <gap:plugin name="org.apache.cordova.inappbrowser" version="0.5.2" />
  <gap:plugin name="org.chromium.zip" version="2.1.0" />
  
  <gap:config-file platform="ios" parent="NSAppTransportSecurity">
      <dict>
          <key>NSAllowsArbitraryLoads</key>
          <true/>
      </dict>
  </gap:config-file>
 
 <allow-navigation href="*" />
 <allow-intent href="*" />
 <access origin="*" />
 
 
</widget>

This is the part of the code snippet to get the local path of the app and loading the path in an iframe:

var appLocalUrl = cordova.file.dataDirectory + AppId + "/index.html";
iframe[0].contentWindow.location.replace(appLocalUrl);

The app is downloaded to local path but when we try to launch this in iframe ended up with error message:

not allowed to load local resource:{file:///var/mobile/Containers/Data/Application/B8BF6DE2-228D-46B9-A84D-D2122D152385/Library/NoCloud/970/index.html}

Any help on this?

解决方案

So I have already struggled a lot related to this. Latest iOS uses WKWebView. It appears to treat local files as if they came from a remote server, even though they're in the app itself, and such requests are blocked. Reference Source

The following solution worked for me:

  1. Add the Corodova file plugin.

    cordova plugin add cordova-plugin-file

  2. Change the local file path to this:

    cdvfile://localhost/bundle/www/you_folder_name/file_name.mp3

这篇关于不允许加载本地资源ios9 cordova应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆