Download localized files from server - ios programming - objective-c

For localized files, for example, localizedthis.png, the path for this file can be retrieved using
[NSBundle mainBundle] pathForResource:ofType:inDirectory:forLocalization:].
The question is, if we download the files from server and store them in "Documents" folder, is there any equivalent way to do something similar to above method?
Thanks.

Bundles are essentially just directories with a specific structure. So you could put the localized files that you download into a directory like Documents/MyBundle/Contents/Resources/fr.lproj/ (for French) and instead of [NSBundle mainBundle], you'd use [NSBundle bundleWithPath:...].

Related

Access XML file in project bundle in Xcode 6.3.2

I am trying to load an XML file in Xcode. The file is supposed to be loaded when the app is started, and updated with data to be saved for future use.
I dragged the file in the "Supporting Files" folder in Xcode, and I can see a copy of the file in the project subdirectory:
/Users/Alex/Documents/ObjectiveC projects/myProject/myProject
The code I am using to access the file is:
// Search for files in bundle
_path = [[NSBundle mainBundle] pathForResource:#"profile" ofType:#"xml"];
NSLog(#"myFile path: %#", _path);
However, when I run the app the _path is set to some other directory:
myFile path: /Users/Alex/Library/Developer/CoreSimulator/Devices
/8F2AD2A7-4875-47C9-9AB8-852339AF31A0/data/Containers/Bundle/Application
/7D9A4A78-A7CB-40E5-8CC7-6B8EE2143D2E/myProject.app/profile.xml
Also, it looks like I cannot write to this file. The content is the same if I restart the app. This is happening regardless if I run the app in the Simulator or the device.
It almost looks like I am accessing some version of the file that was cashed in the bundle when I ran the up for the first time.
It turns out that the files in the bundle are read only. This was discussed in other posts: File write with [NSBundle mainBundle] fails
I obtained what I need by copying the file to a new file when the app is initialized the first time it runs, and then use the new file as read/write file.

How to create and use bundles in cocoa?

I have an application that I wrote that uses an external unix executable file. It needs to run this file in order to get some data needed for further processes.
Right now this executable is located in my project folder and in order to use it in my app I have hardcoded paths to it (which is bad).
I've heard that to avoid this hardcoded paths issue it's possible to use bundles.
Can anyone explain me if this is the best way to achieve what I want, and direct me how to do it if so?!
I already looked through similar questions on stackoverflow and went through this:
https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html#//apple_ref/doc/uid/10000123i
it didn't really help me so far...
You use the NSBundle object to locate your executable. You start by getting your application's bundle using [NSBundle mainBundle]. Depending on where you've placed your Unix tool, you can use NSFundle's pathForAuxiliaryExecutable: or pathForResource:ofType: to locate your executable.
For example, if your Unix tool is in your Application bundle's Resources folder, you could do the following:
NSString* toolPath = [[NSBundle mainBunble] pathForResource:#"toolname" ofType:nil]

Using NSBundle to load my resources

I wanted to separate my resources, nib files and localization files into a common reusable bundle. And so I created a bundle for my ios application and specified resources to be included inside the bundle using build phases, copy bundle resources. But, now if I try to load the bundle, I am not able to load the bundle. I try using [NSBundle allBundles] and the array shows only the main apps bundle.
I also tried to enumerate the directory for NSApplicationPath but again the only bundle available is my application default bundle. I wanted to learn this technique and make use of it to separate my resources. Any help and suggestions would be greatly appreciated. Thank you
[NSBundle bundleWithPath:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] bundlePath], pathToYourBundleWithinTheDotAppDirectory];
Let me know how you get on.
Try something like this:
NSBundle* bundle=[NSBundle bundleWithIdentifier: #"bundle name"];
And make sure that you have selected these options when you have dragged the bundle to the project:
For projects with one bundle, I use:
// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"];
What makes this so convenient is that when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the #"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.

Documents directory in objective C

In Xcode, the Resources folder links to the NSDocumentsFolder of the app? Or what else?
Is there a way to see the files in NSDocumentFolder without write code?
No the resource folder in Xcode does not link to the document directory, it is just imaginary folder for organization in xcode. However there you could see the content of your document directory by browsing to the folder in the simulator.
The folders you use in XCode will not be copied to your application bundle even though all their content (as well as anything else you have in your XCode project...excluding compiled resources) will be flatly copied into your Bundle Folder. If what you need is accessing the Documents folder on an iOS app or in a the User's library you can access it (by code, I'm sorry) with:
NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
And anything you need to be in there you will have to copy by code.

NSTask setLaunchPath Objective-C Cocca

This is a very simple question. I have a script in the same folder as the Cocoa app. I can't seem to set the path to it properly for setLaunchPath for NSTask. Help please.
I have a folder called Project. Inside of it, exist multiple folders but only two we care about: Classes (the source files for the Cocoa app are here) and Ruby (which is a ruby server folder). I am trying to call Ruby/script/server. I assumed it would be something like ./Ruby/script/server or Ruby/script/server but both are wrong.
Thanks.
EDIT: I guess what I'm actually asking is if there is a way to access the folder where the source files or the project is by using a special character or shortcut because by default it goes to the /tmp folder.
The current directory (in the unix sense) in an app is not guaranteed to be anything. The path to the app itself can be obtained by getting the main app bundle by
NSBundle*mainBundle=[NSBundle mainBundle];
and then getting its path
NSString*path=[mainBundle bundlePath];
However please don't do that; you won't be able to distribute your app without extra instructions of putting files here and there.
Instead, put your Ruby code inside yourApp.app/Contents/Resources/. This can be done by including the Ruby code in the XCode, and making sure it's set to be copied into the app. The files inside this Resources directory can be obtained as follows:
NSString*path=[mainBundle pathForResource:#"rubyServer" ofType:#"rb"];
To learn more about the bundle structure, read Bundle Programming Guide.