Document Catalog on iPod Touch and iPhone

I am developing an application that reads data on iPod touch / iPhone that is sent to it via UDP multicast sockets, and I need to save it as a file in the document directory. Does it exist on iPhone or iPod Touch? I know that NSFileHandle and NSFileManager are what I plan to use to take care of reading and writing to a file, but I'm not sure where the "My Documents" section is on iPod touch, if you know what I am, he said. I am not familiar with the iPod / iPhone file directory yet, so any help is appreciated! Is there some kind of “common” directory that all developers use to store their files if they participate in their application?

+3
source share
2 answers

You must use the application directory to store persistent files. You can get the directory path using this function, which Apple includes in its template for an application using Core Data:

/**
 Returns the path to the application documents directory.
 */
- (NSString *)applicationDocumentsDirectory {

    NSArray *paths = 
      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}
+7
source

Most recently, a template for a Core Data application provides this code:

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

If the returned NSArray from NSSearchPathForDirectoriesInDomains is empty, lastObject returns nil, so the result is shorter and cleaner code.

, - iOS 5, , , Documents. . , Caches. , NSDocumentDirectory NSCachesDirectory .

+7

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


All Articles