iOS上的PhoneGap带有资产的绝对路径URL?

PhoneGap on iOS with absolute path URLs for assets?(iOS上的PhoneGap带有资产的绝对路径URL?)

本文介绍了iOS上的PhoneGap带有资产的绝对路径URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 上将 Web 应用程序移植到 phoneGap,我们有绝对路径的资产(和 ajax)URL,例如:

Porting a web app to phoneGap on iOS, we have asset (and ajax) URLs that are absolute paths, e.g.:

<img src="/img/logo.png">

我们将 img 目录打包在 PhoneGap 的 www 目录下.图片不会用绝对路径加载,只能用相对路径加载,例如:

We have the img directory packaged under PhoneGap's www directory. The image will not load with the absolute path, but only with a relative path, e.g.:

<img src="img/logo.png">

有人能解释一下在这种情况下 URL 是如何加前缀或翻译的吗?即:

Could someone please explain how the URL is being prefixed or translated in this context? I.e.:

<img src="/www/img/logo.png">

也不起作用.那么 iOS 上 PhoneGap 使用的 URL base 是什么?

does not work either. So what is the URL base used by PhoneGap on iOS?

我们也试过,例如:

<img src="file://img/logo.png">
<img src="file:///img/logo.png">

但是不行.

我们希望避免将 URL 更改为端口的相对 URL,因为在整个 CSS、Ajax 代码中使用绝对路径 URL,由带有 Rails 后端的 Sprocket 设置等等.我们如何才能打开 PhoneGap/UIWebViewiOS 使用写的绝对路径 URL 加载资产?

We would like to avoid changing the URLs to relative for the port, as absolute path URLs are used throughout the CSS, Ajax code, are set by Sprockets with the Rails backend, etc. How can we just get PhoneGap/UIWebView on iOS to load the assets using the absolute path URLs as written?

我在 StackOverflow 上看到很多人以各种形式提出这个问题,但我还没有看到正确的答案.

I see this question is asked a lot in various forms here on StackOverflow, but I've yet to see a correct answer.

推荐答案

可以通过以下方式在 JavaScript 中获取应用程序的路径:

One can get the path to the application in JavaScript by:

cordova.file.applicationDirectory

由于我在 Android 上,它说:file:///android_asset/" ...例如:

Since I'm on Android, it says: "file:///android_asset/" ...for example:

var img_path = cordova.file.applicationDirectory + 'www/img/logo.png';

这样,在跨平台构建时,所有资源都可以找到.

Like this all resources would be found when cross-building for various platforms.

这篇关于iOS上的PhoneGap带有资产的绝对路径URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS上的PhoneGap带有资产的绝对路径URL?

基础教程推荐