objective c - How do i create a file in the default documents directory in an iOS app -
i'm writing app needs store persistence information in file i'm having trouble debugging createfileatpath:contents:attributes: function of nsfilemanager, have reduced problem below piece of code.
nsfilemanager *filemgr; filemgr = [nsfilemanager defaultmanager]; nsstring * filename = @"newfile.txt"; nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [documentsdirectory stringbyappendingpathcomponent: filename]; nslog(@"full path name: %@", filepath); // check if file exists if ([filemgr fileexistsatpath: filepath] == yes){ nslog(@"file exists"); }else { nslog (@"file not found, file created"); if (![filemgr createfileatpath:filepath contents:nil attributes:nil]){ nslog(@"create file returned no"); } }
since function doesn't take nserror object i'm having trouble finding out why returning false.
from other examples have seen here write file on ios , here how programmatically create empty .m4a file of length under ios using coreaudio on device? should ok pass nil both contents , attributes parameters. i've tried specifying both parameters , receive same result.
i'm thinking may permissions issue i'm unsure if below code correct way check permissions on directory.
if ([filemgr iswritablefileatpath:documentsdirectory] == yes){ nslog (@"file readable , writable"); }
// following code creat directory in documentsdirectory nsstring *docdir = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)[0]; nsstring *dirname = [docdir stringbyappendingpathcomponent:@"mydir"]; bool isdir nsfilemanager *fm = [nsfilemanager defaultmanager]; if(![fm fileexistsatpath:dirname isdirectory:&isdir]) { if([fm createdirectoryatpath:dirname withintermediatedirectories:yes attributes:nil error:nil]) nslog(@"directory created"); else nslog(@"directory creation failed"); } else nslog(@"directory exist");
Comments
Post a Comment