Is it possible to write new file to bundle resource directory in iOS app?

自闭症网瘾萝莉.ら 提交于 2020-01-10 19:14:20

问题


Is it possible to write new file to bundle resource directory in iOS app?


回答1:


No there is no way to write in the bundle dir.. because the bundle dir is code signed with SSL certificate and you can't break it. however you can easily write in the documents directory of you iphone application

You can get the documents directory path using this -

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
return [paths objectAtIndex:0];



回答2:


Or you can copy the resources needed and then operate on files with writing permission.

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

NSString *documentsDir = dirPaths[0];

NSString *databasePath = [documentsDir stringByAppendingString:@"/TwigitDatabase.db"];

NSFileManager *fm = [NSFileManager defaultManager];

if(![fm fileExistsAtPath:databasePath]) {   // Checking whether the my database was copied earlier, though it could be saved in some preferences property.
    NSError *error;
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"TwigitDatabase" ofType:@"db"];
    [[NSFileManager defaultManager] copyItemAtPath:soundFilePath toPath:databasePath error:&error];

    NSLog(@"%@", error);


}


来源:https://stackoverflow.com/questions/5854795/is-it-possible-to-write-new-file-to-bundle-resource-directory-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!