IOS: copy a file in documents folder

前端 未结 3 1732
北海茫月
北海茫月 2020-11-27 15:00

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

3条回答
  •  暖寄归人
    2020-11-27 16:05

    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];
    

提交回复
热议问题