Phonegap on iOS with Absolute Path Urls for Assets

Does Phonegap support root relative path? What are the best practices?

Basically in phone gap development everything that concerns your code resides in the www folder.

-myApp
-www
-index.html
-img
-js
-css
-libraries
-templates

The best would be to just refer the files as js/file.js and css/file.css i.e relative to index.html.

Root relative paths may conflict depending on the platform and thus would be a unnecessary hassle.

Root Relative Paths:

doing something like this :

<link href="/css/app.css">

This will work in your browser if you have a local server setup and have set your myApp/www folder as the root.

But when you build your app in cordova and test it on your phone, it will display incorrectly as it does not have any reference to that server root and will reference it as file:///.

Absolute paths

An absolute path would require you to mention the complete address. When you are creating your app, your code resides in the myApp/www folder. But when you build the app(assuming android), it is moved to the platforms/android/assets/www folder. So your absolute paths will again be wrong.

Remote Server

Your app obviously interacts with a remote server . If you store your images on your remote server, then you must refer to them with absolute paths in your application.

get directory with absolute path in phonegap

Check whether your absolute path has // uri prepended. So your code will look like

window.resolveLocalFileSystemURI("//" + yourpath, onSuccess, onFail);

by prepending to the URI // it looks in the correct path.

phonegap iOS file.path

The problem was, that the new file-plugin has to be case-sensitve.
I created a folder with capital letters and refered the copy-instruction to a folder with lowercase letters. The point is that android is case insensitive and ios is not.
This came out with the new file-plugin where the output was case sensitive.

Get Device absolute path in phonegap?

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function fail() {
console.log("failed to get filesystem");
}

function gotFS(fileSystem) {
console.log("got filesystem");

// save the file system for later access
console.log(fileSystem.root.fullPath);
window.rootFS = fileSystem.root;
}

function downloadImage(url, fileName){
var ft = new FileTransfer();
ft.download(
url,
window.rootFS.fullPath + "/" + fileName,
function(entry) {
console.log("download complete: " + entry.fullPath);

},
function(error) {
console.log("download error" + error.code);
}
);
}

PhoneGap Relative URL

When using PhoneGap the main files will be on the phone so relative files will be relative to the location on the phone.

If you need to access a file on a remote server (your mobile site) then it must be specified absolutely.

If your main HTML page within your PhoneGap app is at file://www/index.html and you try and access a relative file (say "logo.png") and so specify <img src="logo.png" /> you're really getting it from file://www/logo.png.

If you actually wanted the version of logo.png which is actually on your remote website, you have to provide the full (absolute) path or there's no way for the browser to know that when you specify "logo.png" you mean the one at "http://www.your-site.com/logo.png".



Related Topics



Leave a reply



Submit