Read and write file on iPhone

How to do file read / write operation on iPhone? Which path should I specify to save the file? How can I get the current working directory in the iPhone?

thank

+3
source share
1 answer

Write to file:

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

// the path to write file
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];

[data writeToFile:myFile atomically:YES];

Read the file:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];  
if (myData) {  
    // do something useful  
}  
+7
source

Source: https://habr.com/ru/post/1763543/


All Articles