iOS 上的 PhoneGap 具有资产的绝对路径 URL? [英] PhoneGap on iOS with absolute path URLs for assets?

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

问题描述

将网络应用程序移植到 iOS 上的 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">

也不起作用.那么,PhoneGap 在 iOS 上使用的 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 更改为相对端口,因为在整个 CSS、Ajax 代码中使用绝对路径 URL,由带有 Rails 后端的 Sprockets 设置等.我们如何才能获得 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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