简体   繁体   中英

Adding folder to iPad bundle works on simulator, not on device

I have two targets for my app.
Each has its file own Info.plist and a custom folder included (as references, not as group).
When I run my target on the simulator the folder is correctly inserted in the bundle and the files inside it are read without problems.
This is the code to read the folder path:

NSBundle* mainBundle;
NSString *fileFolder;
mainBundle = [NSBundle mainBundle];
NSLog(@"Bundle path: %@", [mainBundle bundlePath]);
NSLog(@"Folder name: %@", [mainBundle objectForInfoDictionaryKey:@"CustomFolder"]);
NSString *folder = [mainBundle objectForInfoDictionaryKey:@"CustomFolder"];
fileFolder = [[[NSBundle mainBundle] pathForResource:folder ofType:nil] retain];
NSLog(@"Folder: %@", fileFolder);

If I try to debug the app on the device, Bundle path and Folder name are correct, but the folder string is null.
I've checked inside my app product, and the there is the right folder, with all the files.
So what's the problem? Is it a read permission issue?

UPDATE The resulting path running in iPad Simulator is this

/ * /Library/Application Support/iPhone Simulator/4.3.2/Applications/8337B9E5-1BFE-4A17-969E-E3E6E43193D6/MyApp.app/CustomFolder/

UPDATE 2 The problem is that I've added the folder with "Create folder references for any added folders".
I replaced pathForResource with stringByAppendingPathComponent, but the problem now persist for subpathsOfDirectoryAtPath.

You have to make sure you use the correct place in the iPad memory. there is no restrictions to where you save stuff on the simulator. This code works

+(NSString*) pathToDocumentsFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;  
}
+(NSString*) pathToFileInDocumentsFolder:(NSString*)filename
{
NSString *pathToDoc = [NSBundle pathToDocumentsFolder];
return [pathToDoc stringByAppendingPathComponent:filename];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM