How can I get my own bundle's infomation? - ios

I want to get some information of "myBundle.bundle" such as version,create time whitch identify the bundle. Thest information may wirite by myself localed in myBundle.bundle.
Are there any recommend method for doing this ?
I try like this, but not success:
I put a plist named Info.plist into the root of myBundle.bundle.
read the info like below,but the info is nil:
self.bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:#"myBundle" ofType:#"bundle"]];
NSDictionary * info = [self.bundle infoDictionary];

You can Use the following line of code to get Bundle Version
NSString * bundleVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"];

NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];
NSString build = infoDictionary[(NSString)kCFBundleVersionKey];
This will give all the info. that is stored in plist like version,bundle version, display name, etc.

Related

Get value from xxx.plist to Build Settings in Target

I have created a custom plist named as test.plist in Supporting Files folder. In that i stored App product name and App version number.
Now, how could i get product name from test.plist for PRODUCT NAME in Build Settings in Target.
I can get values from user defined build settings. But i don't want that.
Note: I need to store product name in test.plist only. And others can access the value from this file.
Thanks in Advance..
you can read the values from the plist in main bundle, put below code in viewDidLoad , method, Please note that all files in main bundle are read only if you want to write any data to plist then you have to copy it to app's document directory
NSString *bundle = [[NSBundle mainBundle] pathForResource:#”test” ofType:#”plist”];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:bundle];
NSString *productName = [dict objectForKey:#"ProductName"];
NSString * appVersion = [dict objectForKey:#"AppVersion"];
Another way is to read app properties
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleDisplayName"];
NSString appVersion = [NSString stringWithFormat:#"Version %# (%#)", [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"], kRevisionNumber];
Hope it helps!

NSBundle pathForResource: returns nil

I am attempting to access a .txt file from my supporting files folder in Xcode on iOS using the following piece of code:
NSString* filePath = #"filename.txt";
NSLog(#"File path: %#", filePath);
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"txt"];
NSLog(#"File root: %#", fileRoot);
The first NSLog prints just what I expect it to print, but the last NSLog always simply prints
File root: (null)
Accessing text from the file after (attempting to) reading it into memory also simply gives me a (null) printout.
The solution for this problem is probably right under my nose, I just can't seem to find it. Any help is very appreciated! Thanks in advance.
filePath already contains the suffix ".txt", therefore you must not specify it
again when locating the resource. Either
NSString* filePath = #"filename.txt";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];
or
NSString* filePath = #"filename";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"txt"];

What is interpretation for bundleForClass returns nil and how that can be corrected?

I am preparing an XCTest and to perform the test I need to load a resource from test bundle. Therefore I use:
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:#"item" ofType:#"plist"];
The problem is that bundleForClass returns nil. How can that be happening, as every class is tied to a bundle which was loaded from? Is there any setting to be corrected to make it working?
You can do like,
NSString *path = [[NSBundle mainBundle] pathForResource:#"item" ofType:#"plist"];

Get and update plist value

I created a plist titled "appData.plist" in my supporting files section of Xcode. I have a key called "keyone" and am trying to get the value of it. I'm trying to log the value to console with NSLog but its not working.
How do you do this? I've searched google and there are a lot of way but none work for me.
NSString *myFile = [[NSBundle mainBundle] pathForResource: #"appData" ofType: #"plist"];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:myFile];
NSString *viewCover = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"keyone"];
This is what i've tried but the whole thing ends in errors, I can't log it and I get an 'unusable variable' error.
The objectForInfoDictionaryKey method retrieves a value from your app's Info.plist file, which isn't what you want -- you want to retreive it from your dictionary myDict. You mentioned that the value is actually a boolean, so replace the last line with
BOOL viewCover = [[myDict objectForKey:#"keyOne"] boolValue];

Not able to retrieve info.plist data by -pathForResource method?

In my application I am not able to get the plist using the below code. I am using Xcode 4.5
and here is my code for retrieving plist,
NSBundle *thisBundle = [NSBundle mainBundle];
NSDictionary *theDictionary;
NSString *commonDictionaryPath;
if (commonDictionaryPath = [thisBundle pathForResource:#"newfonts-Info" ofType:#"plist"])
{
theDictionary = [[NSDictionary alloc] initWithContentsOfFile:commonDictionaryPath];
}
Note:- If I try to retrieve a text or xml file then the above code works fine.
To get the Info plist as a dictionary, you can just do:
NSDictionary *theDictionary = [[NSBundle mainBundle] infoDictionary];
Use
[[NSBundle mainBundle] infoDictionary];
Calling infoDictionary on any bundle instance will return something (thought it may only contain private keys).
Try this code
SArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString* fooPath = [[NSBundle mainBundle] pathForResource:#"newfonts-Info" ofType:#"plist"];
NSLog(fooPath);
contentArray = [NSArray arrayWithContentsOfFile:fooPath];
NSLog(#"%#",contentArray);
or this can also works with NSDictionary
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"newfonts-Info" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(#"%#",dict);
i hope it helps you.
Finally I find the answer by self
I just open the documents directory and open the application.app file and got to see that the plist file which is shown as application-Info.plist in the bundle is seen just as Info.plist
so I changed the above code and and retrieve the dictionary
NSString *commonDictionaryPath;
commonDictionaryPath=[[NSBundle mainBundle]pathForResource:#"Info" ofType:#"plist"];
NSDictionary *info=[NSDictionary dictionaryWithContentsOfFile:commonDictionaryPath];
NSLog(#"path %#",commonDictionaryPath);

Resources