问题描述
在iOS上将网络应用程序移植到phoneGap,我们有资产(和ajax)网址是绝对路径,例如:
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">
我们在PhoneGap的www目录下打包了img目录。图片不会加载绝对路径,但只能使用相对路径,例如:
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基地是什么?
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">
但不行。
我们希望避免将网址更改为相对于端口,因为绝对路径网址在整个CSS,Ajax代码中使用,由Sprockets设置Rails后端等等。我们如何才能在iOS上获取PhoneGap / UIWebView来使用绝对路径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?
我看到这个问题在很多不同的
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.
这篇关于PhoneGap在iOS上有资产的绝对路径URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!