nsfilemanager

Check if two files are the same in Cocoa

旧时模样 提交于 2019-12-19 07:53:46
问题 How do you efficiently check if two files are the same (have the same data) in Cocoa? Context: I'm writing a program that receives a file as input (input file) and copies it into a directory. If the directory already contains a file with the same name (namesake file) then the input file should be copied with a new name only if the namesake file is different. 回答1: you can use -[NSFileManager contentsEqualAtPath:andPath:] . From the Docs: If path1 and path2 are directories, the contents are the

NSFileManager:enumeratorAtURL: returns a different form of URL to NSFileManager:URLForDirectory

寵の児 提交于 2019-12-19 05:22:30
问题 If I fetch my sandbox's Application Support directory using NSFileManager:URLForDirectory then I get back the following URL: file://localhost/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/ However if I use NSFileManager:enumeratorAtURL to search the directory I get back URLs of the form: file://localhost/private/var/mobile/Applications/EA80DE91-394C-4EAB-B269-1081C859BB0F/Library/Application%20Support/ This is causing me problems because I'm doing

What characters are allowed in a iOS file name?

蹲街弑〆低调 提交于 2019-12-18 14:05:15
问题 I'm looking for a way to make sure a string can be used as a file name under iOS. I'm currently in the section of the code that deletes incompatible characters. I'm wondering if I'm doing it right. NSString *filename = @"A file name"; fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet controlCharacterSet]]; fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]]; I'm also wondering if there's already a method that validates a string as a

How to read/write file with iOS, in simulator as well as on device?

两盒软妹~` 提交于 2019-12-18 03:37:18
问题 As I need to read a file when my app is launched and write it sometimes while using it, I tried to reach it with : NSString *dataFile = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"]; NSLog(@"%@",dataFile); And the file, that should be in my project folder, is instead in the simulator folder : 2012-06-13 17:36:56.398 MyFileApp[610:15203] /Users/Rob/Library/Application Support/iPhone Simulator/5.1/Applications/1FFD4436-DCCA-4280-9E47-F6474BEE0183/MyFileApp.app/myFile.txt So if I

How to check if a directory exists in Objective-C

一曲冷凌霜 提交于 2019-12-18 02:02:30
问题 I guess this is a beginner's problem, but I was trying to check if a directory exists in my Documents folder on the iPhone. I read the documentation and came up with this code which unfortunately crashed with EXC_BAD_ACCESS in the BOOL fileExists line: -(void)checkIfDirectoryAlreadyExists:(NSString *)name { NSFileManager *fileManager = [[NSFileManager alloc] init]; NSString *path = [[self documentsDirectory] stringByAppendingPathComponent:name]; BOOL fileExists = [fileManager fileExistsAtPath

How to find out if the NSURL is a directory or not

怎甘沉沦 提交于 2019-12-17 23:25:16
问题 I have a NSURL object. It has address of a filesystem element, it is either a file or a directory. I want to be able to tell if the NSURL is a directory or a file. I have already tried this, which doesn"t seem to work! NSURL * temp ....... ;// it is initialized and has a valid value CFURLRef xx = (CFURLRef)CFBridgingRetain(temp); if(CFURLHasDirectoryPath(xx)) NSLog(@"was a file"); else NSLog(@"was a folder"); 回答1: NSNumber *isDirectory; // this method allows us to get more information about

iPhone (iOS): copying files from main bundle to documents folder causes crash

。_饼干妹妹 提交于 2019-12-17 19:12:38
问题 I am trying to set up my application so that on first launch, a series of files located in the "Populator" folder in the main bundle are copied into the documents directory. My current implementation is as follows: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *sourcePath = [[[NSBundle

Cocoa - Gaining Root Access for NSFileManager

╄→尐↘猪︶ㄣ 提交于 2019-12-17 18:33:41
问题 I need to move system files with NSFileManager in my application and I don't seem to have root access. What would be the easiest way to go about gaining this privilege? I have looked into the BetterAuthorizationSample code provided by Apple, and I don't seem how I could have the NSFileManager run its task once its been given approval by the user. 回答1: Update: To update people still using this answer for reference, BLAuthentication makes use of an old, and highly unrecommended function called

How to save file in the documents folder?

≯℡__Kan透↙ 提交于 2019-12-17 16:42:06
问题 I am trying to save data in the documents folder on iPhone 5.1 simulator. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"]; if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) { NSLog(@"Writable"); }else { NSLog(@"Not Writable"); } I've got always "Not Writable". Any idea? Please

iOS: How do you find the creation date of a file?

我与影子孤独终老i 提交于 2019-12-17 10:25:47
问题 I'm trying to find the creation date (NOT modification date) of a file. Creation date doesn't appear to be in the attributes of a file, though modified date is. I'm using this code.. NSFileManager* fm = [NSFileManager defaultManager]; NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME]; NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil]; if (attrs != nil) { return (NSDate*)[attrs objectForKey: NSFileCreationDate]; } else { return nil; } This always returns