Get name of Xcode Project in actual code - iphone

So you know the part of the project that is "MyNameGoesHere.app" or "MyNameGoesHere.xcodeproj" - is there a way to get the MyNameGoesHere part via objective-c code?
I can get all kind of device info from UIDevice messages but I can't figure out how to get project/app name.

CFBundleDisplayName doesn't work anymore. Use:
NSString *bundleName = [[NSBundle mainBundle]
objectForInfoDictionaryKey:#"CFBundleName"];

You're probably looking at the bundle's InfoDictionary. You can get the app's name via the following code:
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *bundleName = [NSString stringWithFormat:#"%#", [info objectForKey:#"CFBundleDisplayName"]];

Related

Application crashes at [[NSBundle mainBundle] pathForResource

My Application is crashes in the device at this point,
[[NSBundle mainBundle] pathForResource
I am giving the path for the pdf here . but at the execution time it is showing that it is not getting path there . and that may be the reason for crashing.
however it is running perfectly on simulator .
i am not able to figure out why is this happening
here is that code
NSString *path = [[NSBundle mainBundle] pathForResource:[myArray objectAtIndex:0] ofType:[myArray objectAtIndex:1]];
NSLog(#"array elemt :%#", [myArray objectAtIndex:0]);
NSLog(#"array elemt 1 :%#", [myArray objectAtIndex:1]);
NSLog(#"path is :%#",path);
NSLog(#"responds to selector mainBundle=%#",[NSBundle respondsToSelector:#selector(mainBundle)]?#"YES""NO");
NSURL *targetURL = [NSURL fileURLWithPath:path];
in this log of the path - when i use device it is showing me nil and when i use simulator it is showing me the path . and it is showing me that is is crashing on NSURL line
I've found that when something like this works on the simulator, but not on the device, it's likely a problem with the case of the string. Your device is case sensitive but not your computer. Check your string of the file name or the type.

How can I access the application version?

I would like to know how can I access the iOS application target version.
This number : http://img11.hostingpics.net/pics/146187Capturedcran20110728111355.png
Thanks
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* versionNum = [infoDict objectForKey:#"CFBundleVersion"];
This will give the version number. Similarly you can get other attributes as well.

How to create a NSMutableDictionary using dictionaryWithContentsOfFile: method

I want create and return a dictionary using the keys and values found in a file specified by a given path. I have my file on my Desktop:ciudades.txt (a human readable file!!! no a xml, just for practice). What method of my NSString i need to use and how? Please can somebody help me filling on my code the XXXXXXX. Thanks in advance
- (NSMutableDictionary)ciudades
{
if (!ciudades) {
NSString *path = [NSString XXXXXXXXX];
ciudades = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Define a function.
-(NSMutableDictionary*) ReadFileAsDictionaryForPath:(NSString* ) path
{
return [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Use it as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSMutableDictionary* myDictionary = [self ReadFileAsDictionaryForPath:path];
if(!myDictionary)
{
NSLog(#"Error while reading data from file at path:%#",path);
}
First add that file to your application bundle By adding a existing file from xcode to your project. Then use this method to get the file path for example I'm getting a image's path.
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
then try the dictionaryWithContentsOfFile method and see if it works or not.

can't read plist from resources

in my resources directory i have a file called lsf.plist
i want to load this file (dictionary) but i always get null as content of the file. i am using the following code. i've verified that the file is in the app after the build.
self.path = [[NSBundle mainBundle] pathForResource:#"lsf" ofType:#"plist"];
NSLog(self.path);
self.lsf = [NSMutableArray arrayWithContentsOfFile:path];
NSLog(#"%#",lsf);
The first log-output shows the path and the second one gives me null....
it would be great if you can help me to solve this issue!
Br,
martin
Is root of your plist array or dictionary?
Also you dont need to use self.
Heres an example from a project of mine:
NSString* path = [[NSBundle mainBundle] pathForResource:#"property" ofType:#"plist"];
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:path];

Iphone SDK: pathFoResource:Nsstring problem -> playin MP3 files

I am trying to get 10 mp3 files in the resources folder and play them when a button is pressed. I have entered name of the mp3 files in a NSMutableArray and read each one after the button is pressed.
The problem is that pathForResource: is not working (returns nil). If i use the file name explicitly -like pathFoResource:#"song1.mp3" ofType:#"mp3"- but if i use pathForResource:valuem whwre valuem is an NSstring with value of song1
Hope you can help
Regards
NSString *cellValue0 = [listOfmp3 objectAtIndex:anumber];
NSString *valuem;
if ( [cellValue0 length] > 0 )
valuem = [cellValue0 substringToIndex:[cellValue0 length] - 4];
NSString *pdfPath2 = [[NSBundle mainBundle] pathForResource:valuem ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:pdfPath2];
Don't put the extension in the resource name, i.e.
[bundle pathForResource:#"song1" ofType:#"mp3"]; // correct
[bundle pathForResource:#"song1.mp3" ofType:#"mp3"]; // wrong — gets song1.mp3.mp3.
Check that valuem does not carry any extensions.
Sorry for my previous post, here is restatement of the problem in more clear terms:
Vladimir:
yes i log the valuem and it is giving the correct string
KennyTM:
Yes, it was a typo , in original code there is only #"song1"
the problem is that
if I use
[[NSBundle mainBundle] pathForResource:"song1" ofType:#"mp3"]; It is working
BUT If i use
[[NSBundle mainBundle] pathForResource:valuem ofType:#"mp3"];
It is NOT working
note that value is a string with #"song1"