Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Last active December 11, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbahrenburg/15901e38cf8690680da3 to your computer and use it in GitHub Desktop.
Save benbahrenburg/15901e38cf8690680da3 to your computer and use it in GitHub Desktop.
Adding isDirectory() and isFile() to iOS's TiFileSystemFileProxy
/*jslint maxerr:1000 */
//Test if the resources folder is a directory.
var resourceDir = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory);
Ti.API.info('resourceDir ' + resourceDir);
Ti.API.info('resourceDir nativePath ' + resourceDir.nativePath);
Ti.API.info('resourceDir isDirectory ' + resourceDir.isDirectory());
Ti.API.info('resourceDir isFile ' + resourceDir.isFile());
var newDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'mydir');
if(!newDir.exists()){
Ti.API.info("Created mydir: " + newDir.createDirectory());
}
var newFile = Titanium.Filesystem.getFile(newDir.nativePath,'newfile.txt');
newFile.write("\nText appended via write()", true);
Ti.API.info('newdir ' + newDir);
Ti.API.info('newdir nativePath ' + newDir.nativePath);
Ti.API.info('newdir isDirectory ' + newDir.isDirectory());
Ti.API.info('newdir isFile ' + newDir.isFile());
Ti.API.info('newFile ' + newFile);
Ti.API.info('newFile nativePath ' + newFile.nativePath);
Ti.API.info('newFile isDirectory ' + newFile.isDirectory());
Ti.API.info('newFile isFile ' + newFile.isFile());
Ti.API.info('Now open a window so we can test on device easier');
// Create a simple window to show our results
(function(){
var win = Ti.UI.createWindow({
backgroundColor:'#fff',layout:'vertical'
});
win.add(Ti.UI.createLabel({
top:10, text:'resourceDir isDirectory ' + resourceDir.isDirectory()
}));
win.add(Ti.UI.createLabel({
top:10, text:'resourceDir isFile ' + resourceDir.isFile()
}));
win.add(Ti.UI.createLabel({
top:10, text:'newdir isDirectory ' + newDir.isDirectory()
}));
win.add(Ti.UI.createLabel({
top:10, text:'newdir isFile ' + newDir.isFile()
}));
win.add(Ti.UI.createLabel({
top:10, text:'newFile isDirectory ' + newFile.isDirectory()
}));
win.add(Ti.UI.createLabel({
top:10, text:'newFile isFile ' + newFile.isFile()
}));
win.open();
})();
Ti.API.info('End Test');
//Paste the following into your TiFileSystemFileProxy.m file
-(id)isFile:(id)unused
{
BOOL isDirectory;
return ([fm fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory);
}
-(id)isDirectory:(id)unused
{
BOOL isDirectory;
return ([fm fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment