NSBundle doesn't load dynamic resources - ios

I have a problem reading a Strings file created dynamically inside my app's document directory.
Basically, I create and read my file using:
[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileData attributes:nil];
NSLog(#"%#", [targetBundle localizedStringForKey:#"MY_STRING" value:#"#" table:#"test"]);
The file is created in the document directory, and the targetBundle also points to this directory.
The above code works very well when the strings file already exists in the document directory when the app runs.
So, if I delete the Strings file from the document directory, the NSLog displays: #.
When I run again the application (the file already exists), NSLog displays MY_LABEL's value.
I guess that the bundle loads its resources once at the launch, but how can I be sure to read my new file even if it has just been created?
Thank you for your help,
Julian

I didn't find how to reload a bundle but I fixed this problem by creating an empty file with the same name BEFORE the creation of the bundle.
With that, the bundle "knows" the file and if the file dynamically changes, the content is up-to-date (because the file is read at runtime).

Related

Change file name in UIDocumentInteractionController when opening file in another app

I'm storing files in my application sandbox in a way that masks the original name of the file.
For example I have a file called abc.png which is stored in the sandbox as obfuscated.png.
When I do an open in of this file in another application using a UIDocumentInteractionController I'd like to have the other file open the file with the filename abc.png
Currently the other app opens the file as obfuscated.png.
I have tried changing the name property of the UIDocumentInteractionController in documentInteractionControllerWillPresentOptionsMenu as well as willBeginSendingToApplication, however in both cases the receiving application does not get the correct filename - it continues to show the obfuscated filename.
Apart from creating a copy of the file with the unobfuscated name, is there a way to make the receiving application use the desired filename?
Instead of a copy try:
NSError *error = nil;
[[NSFileManager defaultManager] linkItemAtPath:obfuscatedFilePath toPath:abcFilePath error:&error];
This will create a hard link to the file. Symbolic links will not work.

NULL when using fopen with xcode

I am trying to initialize a multidimensional array from a file using C for a iPhone 4inch app but I can't open up the file using fopen.
Whenever I try this I get a NULL:
FILE *f;
f=fopen("/level1.rez", "r");
if (f == NULL)
{
printf("Error Reading File\n");
//exit (0);
}
I am not sure how to open files using C.
I tried this already:
I printed out the current working directory using getcwd but all I got was "/" and when I attached that to the file name I still got NULL.
I read that if you go to product > scheme > edit scheme then options you can change the current working directory but I don't see that option.
Also I read that you can use absolute paths like: /users/name/desktop/program
but I am new to iOS development so I don't know if that is a good idea.
So how do I get fopen to work?
You CAN specify absolute paths in iOS, but the path in your example is probably used in Mac OS, which is laid out a little differently. You can specify paths to fopen() as you say, but there is more work to finding out what the first part of that path really is.
The iOS puts all AppStore apps into folders with randomly generated sandbox directory names. It is basically the the hexadecimal string of a GUID. So you need to use methods from iOS frameworks to get the first part of the path (or URL) to your file.
If the file is part of the app bundle so it can ship with the app, then you will need to use NSBundle methods to find the path to the file.
If the file is generated or downloaded after the app starts up on the device, then you need to use NSFileManager methods to determine the path to the directory of the file. (Typically the Documents directory. You can build a directory structure of your choice within the sandbox.)

C FOpen() Open Documents Directory iOS

I need help getting access to the documents directory using only C on iOS.
I have my .c file looking for a specific file in the application bundle. I have no problem accessing this file. It looks like this: fopen("filename",
Unfortunately, if I want to move that file to the documents directory, appending "/Documents/filename" doesn't work.
I know how to access the file using an objective-c class, easily, using filesystemrepresentation. But I don't know how to do it only in C. Any help would be greatly appreciated!
Thanks!
Fopen defaults to the directory that the executable file is in, ".app/" - on iOS.
I was able to get to the documents directory by making a char of the Current Working Directory and then removing the last couple characters of the char to get out of the ".app/" bundle and then appending the Documents path to the end of it.

office documents won't open with quicklook

I've created 2 test applications:
In the one the documents are included into the bundle and can be opened by using the quick look controller.
In the other app, the documents are downloaded. When I try to open the files the quick-look controller simply shows a message saying an error occurred. Strangely it will open downloaded PDF documents without any problems.
I suspect it has something to do with headers and mime-types, but I'm not sure on this. Would anyone know what the reason could be that downloaded .doc or .docx documents won't open and local documents will?
It turned out to be a problem with saving the downloaded files. At first I was writing the data to a filepath URL. This saved the data to the right location, but the type of file wasn't recognized anymore.
The solution was to use the default FileManager to save the file.
[[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];

SQLite file is not found when i run on the device

I have an SQLite database file embedded into my project. On the simulator, querying the database works fine, but not on the device. My database file path when i run on the device is this (as shown on the console with an NSLog) :
The path for the database file is : /var/mobile/Applications/5914F328-148F-52E6-1AC9-38D7FF141F9B/MyApplication.app/db.sqlite
My relevant code which looks for the DB is:
NSFileManager *fileMgr=[NSFileManager defaultManager];
NSString *dbPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"db.sqlite"];
NSLog(#"The path for the database file is : %#",dbPath);//Display the whole path
I know that the path on the simulator and on the device are completely different. So how should i change my code above to find my database file relatively?
I solved my issue, for whom that struggle on the same problem, it needs me just to include my SQLite file db.sqlite into my Bundle resources. CLick your project target-->Build Phases--->Copy Bundle Resources and then hit the + button and add your SQLite file or just drag your SQLite file from project Group and files and drop it there. Hope this help :)

Resources