简体   繁体   English

如何在应用程序捆绑包中编辑文件?

[英]How can I edit a file in app bundle?

In my app, I load data from a CSV file stored in the bundle resources. 在我的应用程序中,我从捆绑资源中存储的CSV文件加载数据。 However, I'd like to be able to update this file programmatically when the user taps the Update button. 但是,当用户点击“更新”按钮时,我希望能够以编程方式更新此文件。 Is there a way to change a resource in the app bundle programatically? 是否可以通过编程方式更改应用程序捆绑包中的资源? Here is the code I use to access the file: 这是我用来访问文件的代码:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"query_result" ofType:@"csv"];
    NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path];

First of all load file from bundle and after that just store in Document directory then you can make change on this file again save on document directory to access changed file. 首先从包中加载文件,然后将其存储在文档目录中,然后您可以对该文件进行更改,然后再次保存在文档目录中以访问更改的文件。

Use this code copy from mainbundle to Document. 使用此代码从mainbundle复制到Document。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
NSLog(@"Datapath is %@", dataPath);

// If the expected store doesn't exist, copy the default store.    
    if ([fileManager fileExistsAtPath:dataPath] == NO)
    {
        NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"eng" ofType:@"traineddata"];
        [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:&error];

    }

-(NSString*) applicationDocumentsDirectory{
    // Get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    return docsDir;
}

And then get the Saved file to change... 然后获取保存的文件进行更改...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);

NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何加密iOS应用程序捆绑包中的文件以防止数据复制? - How can I encrypt file in the iOS app's bundle to prevent data from copy? 我可以将亵渎词文件放在应用程序包中吗? - Can I put profanity words file inside app bundle? 如何将 bundle 添加到 react-native App - How can I add bundle into react-native App 如何在iPhone中的应用程序包之外访问文件 - How can I access files outside app bundle in iPhone 无法编辑捆绑包标识符 - Can't edit Bundle Identifier 当我的iPAD上安装了应用程序的'.ipa'文件时,如何获取IOS应用程序的Bundle ID - How to obtain Bundle ID for an IOS App when I have a '.ipa' file for the app installed on my iPAD 为什么我可以播放我的应用程序包中的视频,但不能播放下载到文件系统的视频? - Why can I play videos from my app bundle but not videos that were downloaded to the file system? 如何访问 Swift 中应用程序包中包含的文件? - How to access file included in app bundle in Swift? 如何在我的settings.bundle中使用UIPickerView,例如“ nike + ipod”应用程序是如何做到的? - How can I use a UIPickerView in my settings.bundle like how the 'nike+ ipod' app does it? 应用程序包中的文件路径 - Path to file in app bundle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM