Reputation: 529
I have specific requirement in mobile application for file storage. I am creating one file to store some data into it and that data will be retrieved later.
In Android if I store the file in below location, then file will be deleted while removing the app from android device.
Android/data//files/test.txt
Question:
In IOS device, I don't get this kind of file path where I can store the file to delete while removing the APP.
Can anyone help me where I can store the file to delete automatically while removing the APP?
Upvotes: 4
Views: 1296
Reputation: 529
Here is the final solution after my research and testing in both Android and IOS devices.
Step1: Differentiate the file path based on plat form like below:
device.platform.toLowercase();
FileUtility.prototype.getFilepath = function(){
if(device.platform.toLowercase == "ios"){
return "gsmData.txt"; }
else if(device.platform.toLowercase == "android")
{ return
"Android/data/test.app.com/files/testData.txt"; } };
Above code will return "ios" or "android" based on device.
Step2: Create the file with the path which you get from above function
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
readFileSystem, failureCallBack);
function readFileSystem(fileSys){
fileSys.root.getFile(getFilepath(), {create: false,exclusive:false}, function(fileEntry){
fileEntry.remove(successCallBack,failureCallBack);
}, failureCallBack);
}
In IOS device, if you create the file with your APP then that file will be deleted automatically when you remove the APP from IOS device.
But in Android , created files will be deleted if you store them APP related folders like I have mentioned above.
Hope it helps :)
Upvotes: 3
Reputation: 576
Whenever an app gets deleted by the user or system anything that is stored within its little filesystem sandbox will be automatically deleted with the app. Use the first object of the array returned by
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
to get the path to your very own Document directory where you can write your file in. The whole directory will be deleted when the app is removed from the device.
Upvotes: 1
Reputation: 4047
On iOS, your app doesn't really get access to the file system. It works inside a Sandbox, which is available only to your app. Meaning that if you save a document in the Documents directory, for example, you don't really save it in THE Documents directory, you save it in your app's Documents directory. Only your app will have access to that app, and once your app is uninstalled, the operating system removes all of the items in your app's sandbox along with the app itself.
That is why when you access the Document directory, you don't access a constant directory, you ask the operating system for the specific address of the directory want to use. The physical address of that directory may change between installations (or upgrades) of your app:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"fileName.txt"];
Upvotes: 5