I have a plist in my Xcode project's resource folder but on physical drive it's in the top folder of my project. I want to read and write from this same file. Using NSBundle reads it properly and gives no problem. I'm using this code to read file path:
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"plist"];
but i think NSBundle doesn't allow writing back and so it didn't work when i tried to write back the file. Using the following code creates/ updates file using application path
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
NSString * docsDirectory = [pathArray objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:#"MyFile.plist"];
but for now, I need to write on internal file. Any solution to access it? Besides, I've tried giving path from my working directory to project directory. It also doesn't work.
It's not NSBundle that doesn't support writing, it's iOS itself. iOS will not let your app write to its own bundle.
Related
I have a folder named 'Images' present in my project .The path to that folder is '/Users/username/Documents/Test/ProjectName/Images'.
How to access the images folder in cocoa app.
Thanks
If you really need to work with files and folders in bundle of your app, you could access resources folder with call:
NSURL *resourceURL [[NSBundle mainBundle] resourceURL];
Than you could work with resourceURL, reading content of folder with help of NSFileManager, as you do with regular folders and files on disk.
But that's barely needed. Usually you take resource from bundle by file URL which you obtain with call like:
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:#"user agreement" withExtension:#"rtf"];
Have a look in documentation about other variants to take paths for resources from app bundle.
You can use directly by this
NSButton *closeButton = [[NSButton alloc]initWithFrame:CGRectMake(185,215 , 15, 15)];
[closeButton setImage:[NSImage imageNamed:#"close.png"]];
the close image is now displaying in the button..
this may help you...
I have a sanboxed mac app with com.apple.security.assets.pictures.read-write entitlement set to YES. Hence, I can access users actual /Users/username/Pictures directory.
I would like to get the path of this directory programmatically, how can I do that?
I currently do the following but it results in the sandbox container Pictures location.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
//this gets the pictures directory in sandbox container not the actual user Pictures directory
NSString *userPicturesPath = [paths objectAtIndex:0];
It seems to me that the sandbox container Pictures directory is just an symlink to ~/Pictures. So I believe this is exactly what you want. You can see this by calling stringByResolvingSymlinksInPath on the userPicturesPath in your example. You can see that these files are really just symlinks to the corresponding files in your $HOME
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.
How can I get the current path of my .app which is being executed in cocoa mac application?
I tried using NSTask to do pwd but returns me /
I found it by using [[NSBundle mainBundle] bundlePath]
I'm creating a little program to copy the Domain file for iWeb over to a USB stick or external harddrive or such. The program is meant to be run from that USB stick or external harddrive, and then create a directory where the application is run from. E.g. the application is run from ~/Documents, the application should create a folder at ~/Documents/(account name)'s website, and then copy the Domain file to that folder. But when I try to run the application from a USB stick, it creates a folder under /, called /(account name)'s website. How do I fix this?
If you want the current working directory of your app then use NSFileManager's currentDirectoryPath.
NSString *currentPath = [[NSFileManager defaultManager] currentDirectoryPath];
NSBundle has an instance method called bundlePath which will almost get you what you want.
NSString *bundleParentPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
This should return the directory that the application is being run from.