ios - Error creating a temp file using NSTemporaryDirectory -


in ios 9.1 there's apparently problem following code. doing wrong?

nsstring *filepath = [nstemporarydirectory() stringbyappendingpathcomponent:@"foobar.dat"]; nsstring *s = @"testing"; nsdata *data = [s datausingencoding:nsutf8stringencoding]; nserror *error = nil; bool ok = [data writetofile:filepath options:nsdatawritingatomic error:&error]; if(!ok) {     nslog(@"=== error writing path %@, %@", filepath, error.debugdescription); } 

the log says

=== error writing path /var/folders/gf/4429772177s9rr234wzlwpz00000gp/t/foobar.dat,  error domain=nscocoaerrordomain code=4 "the folder “foobar.dat” doesn’t exist."  userinfo={nsurl=file:///var/folders/gf/4429772177s9rr234wzlwpz00000gp/t/foobar.dat,  nsunderlyingerror=0x15e0bc20 {error domain=nsposixerrordomain code=2 "no such file or directory"}, nsuserstringvariant=folder} 

this happens on iphone, not in simulator. there's gb of free space in phone. help.

edit added code check if temp directory exists, , if not, create it:

nsstring *dirpath = nstemporarydirectory(); nsfilemanager *fm = [nsfilemanager defaultmanager]; bool isdir = no; nserror *error = nil; if (! [fm fileexistsatpath:dirpath isdirectory:&isdir]) {     nslog(@"directory not exist @ dirpath %@", dirpath);     bool success = [fm createdirectoryatpath:dirpath                  withintermediatedirectories:yes attributes:nil error:&error];     if (!success) {         nslog(@"=== error: %@", error.debugdescription);     } } 

again, error occurs on test iphone 5c, not on simulator. logs

directory not exist @ dirpath /var/folders/gf/4429772177s9rr234wzlwpz00000gp/t/ === error: error domain=nscocoaerrordomain code=513 "you don’t have permission save file “t” in folder “4429772177s9rr234wzlwpz00000gp”."      userinfo={nsfilepath=/var/folders/gf/4429772177s9rr234wzlwpz00000gp/t/, nsunderlyingerror=0x1765cdf0 {error domain=nsposixerrordomain code=1 "operation not permitted"}} 

the error (513 = bad permissions) makes sense if last path component ('t') being treated file name, error says. problem creating folder seems similar of writing file, namely last path component being misinterpreted. checked trailing '/' in dirpath string last character, , is.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -