How can I access the application version? - iphone

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.

Related

Get name of Xcode Project in actual code

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"]];

localization from inside the application in iphone

how can we localized our application from the button in built in application not from iphone setting. please suggest me something i am stuck in it. Thanx in advance
You change the bundle before you call for the translation;
NSString *bundle_path = [[NSBundle mainBundle]
pathForResource:#"Localizable"
ofType:#"strings"
inDirectory:nil
forLocalization:#"se"];
NSBundle *localized_bundle = [[NSBundle alloc]
initWithPath:[bundle_path stringByDeletingLastPathComponent]];
NSString* translated = NSLocalizedStringFromTableInBundle(#"KEY", nil, localized_bundle, nil);

Read version from Info.plist

I want to read the bundle version info from Info.plist into my code, preferably as a string. How can I do this?
You can read your Info.plist as a dictionary with
[[NSBundle mainBundle] infoDictionary]
And you can easily get the version at the CFBundleVersion key that way.
Finally, you can get the version with
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* version = [infoDict objectForKey:#"CFBundleVersion"];
for Swift users:
if let version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") {
print("version is : \(version)")
}
for Swift3 users:
if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") {
print("version is : \(version)")
}
I know that some time has passed since the quest and the answer.
Since iOS8 the accepted answer might not work.
This is the new way to do it now:
NSString *version = (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
Now in iOS 8 both fields are necessary. Earlier it works without the CFBundleShortVersionString. But now it is a required plist field to submit any app in app store. And kCFBundleVersionKey is compared for uploading every new build, which must be in incremental order. Specially for TestFlight builds. I do it this way,
NSString * version = nil;
version = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
if (!version) {
version = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
}
Swift 3:
let appBuildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String

Differences in accessing resources between Simulator and Device?

Is there some difference between the way that bundle resources can be accessed on the iPhone simulator versus a device (in my case, an iPad)? I am able to access a file on the former, but not the latter.
NSString *filePath = [NSString stringWithFormat:#"%#%#",[[NSBundle mainBundle] bundlePath], #"/AppResources/html/pages/quickHelp.html"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
// fileExists == YES in the debugger on both the simulator and the device
NSString *path = [NSString stringWithFormat:#"AppResources/html/pages/%#", contentsFileName];
NSString *pathForURL = [[NSBundle mainBundle] pathForResource:path ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:pathForURL isDirectory:NO];
The code above works fine in the simulator, but on the device pathForResource:path returns nil, so the last line throws a 'nil string parameter' exception.
Am I missing something obvious?
edit: Of course, in the above #"quickHelp" is being passed in the contentsFileName var.
edit2: if it makes any difference, in my build settings "Base SDK" is set to "iPhone Device 4.0", and "iPhone OS Deployment Target" is set to "iPhone OS 3.1.3". Any other settings that might have an influence?
[ignore]
I notice a leading '/' on your *filePath but none exists on *path (before AppResources)
[/ignore]
[edit]
[[NSBundle mainBundle] pathForResource:#"quickHelp" ofType:#"html" inDirectory:#"AppResources/html/pages"]]
[/edit]
I believe the filesystem on the iPhone is case-sensitive. Check your cases with the actual files.

How to access files in interior of an iPhone App?

I need in a iPhone app to access files that the app is build with(.plist etc). There's an hardcoded way to do this:
NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
objectAtIndex:0]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:appFolder];
where appFolder is the name of folder app, like "test.app". After the appDir is known, to access files is simple.
Is there any other, not-hardcoded way to have access to files form the app?
Thanks in Advance!
NSString* pathToFile =
[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"myFile.plist"];
// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:#"myFile"
ofType:#"plist"];
Your app folder is the "main bundle". So you can use NSBundle methods such as
NSString* path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"plist"];