不允许加载本地资源ios9 cordova应用程序

2024-03-12

<!--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="[email protected] /cdn-cgi/l/email-protection" 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>

这是获取应用程序的本地路径并将该路径加载到 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。它似乎将本地文件视为来自远程服务器,即使它们位于应用程序本身中,并且此类请求被阻止。参考Source https://www.scirra.com/blog/ashley/25/hacking-something-useful-out-of-wkwebview

以下解决方案对我有用:

  1. 添加 Cordova 文件插件。

    cordova plugin add cordova-plugin-file

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不允许加载本地资源ios9 cordova应用程序 的相关文章

随机推荐