Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: copy a file in documents folder

Tags:

xcode

ios

bundle

In my project I have two file .txt (in Resources folder), how can I copy them inside documents folder?

like image 412
cyclingIsBetter Avatar asked Jul 01 '11 07:07

cyclingIsBetter


People also ask

Does iPhone have a Documents folder?

The iphone does not have a docs folder. Any storage that this app offers is inside the app, likely on a server. You can go to the itunes page from which you downloaded the app and click the link to the Apps support site. Yes, the files will be on the app's folder.

Can you Drag and drop files to iPhone?

With drag-and-drop on the iPhone, you can move seamlessly between apps to quickly upload photos, send links, share files and more. In a few seconds, you can quickly drag a dozen photos to send via text message or drop a link into an email.


3 Answers

Copies txtFile from resource to document if not already present.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];

if ([fileManager fileExistsAtPath:txtPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
}

If you want to overwrite every time then try this:

if ([fileManager fileExistsAtPath:txtPath] == YES) {
    [fileManager removeItemAtPath:txtPath error:&error];
}

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
like image 78
taskinoor Avatar answered Oct 10 '22 15:10

taskinoor


Swift 3

code for file is exist or not, if not then copy.

func CheckFileisExistOrNot(strPath:String) {
    let filemgr = FileManager.default
    if !filemgr.fileExists(atPath: strPath) {
        let resorcePath = Bundle.main.path(forResource: "\(Global.g_databaseName)", ofType: ".db")
        do {
            try filemgr.copyItem(atPath: resorcePath!, toPath: strPath)
        }catch{
            print("Error for file write")
        }
    }
}
like image 40
Ilesh P Avatar answered Oct 10 '22 17:10

Ilesh P


NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0]stringByAppendingString:@"csvfile"];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"sample.csv"];

if ([fileManager fileExistsAtPath:txtPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@".csv"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
like image 1
sagar modi Avatar answered Oct 10 '22 17:10

sagar modi