iphone - NSFileManager Can't retrieve files -
in method transmitdata, nsarray files coming nil. problem in code? save file (we hit [file writetourl: fileurl atomically: yes]) , call transmitdata, cannot retrieve files.
#import "filemanagerservice.h" #import "synthesizesingleton.h" @implementation filemanagerservice synthesize_singleton_for_class(filemanagerservice); #define savevoter @"sv" #define savescript @"ss" - (nsurl *)applicationdocumentsdirectory { return [[[nsfilemanager defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask] lastobject]; } -(void)savefile:(nsdata*)file:(nsstring*)datatype { nsfilemanager *manager = [nsfilemanager defaultmanager]; nsurl *fileurl; nsstring *filename; if (datatype == data_save_voter) { filename = [nsstring stringwithformat:@"%@01",savevoter]; fileurl = [[self applicationdocumentsdirectory]urlbyappendingpathcomponent:filename]; int fid = 0; while ([manager fileexistsatpath:[fileurl absolutestring]]) { fid++; filename = [nsstring stringwithformat:@"%@%d",savevoter,fid]; fileurl = [[self applicationdocumentsdirectory]urlbyappendingpathcomponent:filename]; } } else if (datatype == data_save_script) { filename = [nsstring stringwithformat:@"%@01",savescript]; fileurl = [[self applicationdocumentsdirectory]urlbyappendingpathcomponent:filename]; int fid = 0; while ([manager fileexistsatpath:[fileurl absolutestring]]) { fid++; filename = [nsstring stringwithformat:@"%@%d",savescript,fid]; fileurl = [[self applicationdocumentsdirectory]urlbyappendingpathcomponent:filename]; } } [file writetourl:fileurl atomically:yes]; } -(void)transmitdata { nsarray *files = [[nsfilemanager defaultmanager] contentsofdirectoryatpath:[[self applicationdocumentsdirectory]absolutestring] error:null]; if (files == nil || [files count] <= 0) { nslog(@"no files transmit"); return; } (id file in files) { nsdata * dt = file; nsstring *str = [[nsstring alloc]initwithdata:dt encoding:nsutf8stringencoding]; nslog(@"datacontents: %@",str); } } @end
edit: upon more testing :
filename = @"testfile"; fileurl = [[self applicationdocumentsdirectory]urlbyappendingpathcomponent:filename]; nslog(@"url1: %@",[fileurl absolutestring]);
my log coming null. cant retrieve files because saving null file location....why happening?
fixed:
so have no idea why, [file writetourl:fileurl atomically:yes] not work , [[nsfilemanager defaultmanager] createfileatpath:fileurl contents:file attributes:nil]; work.
use
- (nsurl *)applicationdocumentsdirectory { nsarray *searchpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentpath = [searchpaths objectatindex:0]; return [nsurl fileurlwithpath:documentpath]; }
and
nsarray *files = [[nsfilemanager defaultmanager] contentsofdirectoryatpath:[[self applicationdocumentsdirectory]path] error:null];
Comments
Post a Comment