简体   繁体   中英

iPhone writeToFile doesn't work

I have written one application that save txt file into device. When I test on simulator, I am able to read and write file. After I test on device, read is working fine but it doesn't write anything on that text file. I have searched for the solution but it seems like it has problem with right access. Anyone can suggest me how to make this work?

What path are you using to save the file to?

I'm using

- (NSString *)dataFilePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    return [documentsDirectory stringByAppendingPathComponent:@"myFileNameHere.txt"];

}

and then pass [self dataFilePath]; into the writeToFile method as the path

On real device you don't have access to write everywhere. See "Commonly Used Directories" in Developers Guide - you should write to Documents or Caches directories ( how to get their paths )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM