nsfilemanager

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

牧云@^-^@ 提交于 2019-11-29 01:59:53
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 want to read/write it in using the simulator as well as the real device, what should I do ? Thanks for

Save file in document directory in swift 3?

五迷三道 提交于 2019-11-29 01:08:33
I am saving files in a document directory in swift 3 with this code: fileManager = FileManager.default // let documentDirectory = fileManager?.urls(for: .documentDirectory, in: .userDomainMask).first as String var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String path = path + name let image = #imageLiteral(resourceName: "Notifications") let imageData = UIImageJPEGRepresentation(image, 0.5) let bool = fileManager?.createFile(atPath: path, contents: imageData, attributes: nil) print("bool is \(bool)") return true But as you can see, I am not

Copy Folder (w/contents) from bundle to Documents directory - iOS

混江龙づ霸主 提交于 2019-11-29 00:52:06
问题 EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the file even existed in my bundle - and it did not! So by using this code (also below): iPhone/iPad: Unable to copy folder from NSBundle to NSDocumentDirectory and the instructions for adding a directory properly to Xcode (from here and below)I was able to get it to work. Copy folder into Xcode: Create a Directory on your Mac. Select Add Existing Files to your project Select the Directory you want to import In the pop

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 23:27:47
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"); NSNumber *isDirectory; // this method allows us to get more information about an URL. // We're passing NSURLIsDirectoryKey as key because that's the info we want to know. // Also, we pass

How to check if a directory exists in Objective-C

眉间皱痕 提交于 2019-11-28 22:49:29
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:path isDirectory:YES]; if (fileExists) { NSLog(@"Folder already exists..."); } } I don't understand

Displaying file copy progress using FSCopyObjectAsync

让人想犯罪 __ 提交于 2019-11-28 20:54:53
It appears after much searching that there seems to be a common problem when trying to do a file copy and show a progress indicator relative to the amount of the file that has been copied. After spending some considerable time trying to resolve this issue, I find myself at the mercy of the StackOverflow Gods once again :-) - Hopefully one day I'll be among those that can help out the rookies too! I am trying to get a progress bar to show the status of a copy process and once the copy process has finished, call a Cocoa method. The challenge - I need to make use of File Manager Carbon calls

Get file path by file name from documents directory ios

偶尔善良 提交于 2019-11-28 19:33:18
In my application I download PDF files which gets stored in "Document" directory under different sub folders. Now I have file name for which I want to get its path in "Document" directory but problem is I don't know the exact sub folder under which that file is stored. So is there any method which will give me file path by file's name like there is one method which works for main bundle: (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension I don't want to iterate through each folder which is a tedious way. Thanks. Abhi Beckert You can search the documents directory like

Delete files from directory inside Document directory?

爱⌒轻易说出口 提交于 2019-11-28 18:28:32
问题 I have created a Temp directory to store some files: //MARK: -create save delete from directory func createTempDirectoryToStoreFile(){ var error: NSError? let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) let documentsDirectory: AnyObject = paths[0] tempPath = documentsDirectory.stringByAppendingPathComponent("Temp") if (!NSFileManager.defaultManager().fileExistsAtPath(tempPath!)) { NSFileManager

What's a quick way to test to see a file exists?

隐身守侯 提交于 2019-11-28 16:02:16
I want to quickly check to see if a file exists in my iPhone app's Documents directory (or any path for that matter). I can enumerate through the directory's files, or I can try to open a specific file. What's the fastest way? I just need to know if the file is there or if it does not exist. rein Swift v3: let fileExists = FileManager.default.fileExists(atPath: somePath) Thanks to Nikolay Suvandzhiev . Objective-C (Original): BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:somePath]; 来源: https://stackoverflow.com/questions/1356701/whats-a-quick-way-to-test-to-see-a-file

How to use iCloud to store and sync app files

≯℡__Kan透↙ 提交于 2019-11-28 15:31:20
问题 I already have an iPhone App that stores data in a file in the local documents folder. Now I learnt about iCloud technologies and my first question was: is there a way to use iCloud as a directory when sometimes I check for new versions? I mean: can I avoid using UIDocument, file coordinators and file presenters? I want just to know if could treat iCloud like a special folder and only use NSFileManager to push and retrieve files. Last note: I don't use Core Data or any database, I only have a