App subversion to float - ios

I am getting my app's version like this:
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"]
And then trying to convert it to a float like this:
[version floatValue]
Now that all works great, but when I have a minor version like "1.1.1" everything behind the second decimal point is truncated.
What is the best way to keep everything behind the second decimal point?

To keep it in sync with how you started...
int major, minor, bug;
NSArray *versionItems = [version componentsSeparatedByString:#"."];
major = [[versionItems objectAtIndex:0] intValue];
minor = [[versionItems objectAtIndex:1] intValue];
bug = [[versionItems objectAtIndex:2] intValue];
But I'd recommend looking at this..
How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

Related

Dynamically format a float in a NSString for iOS

I am having the same issues as here: Dynamically format a float in a NSString.
I have searched my hardest for the answer but everything I do seems to break it.
I have tried the following code:
cell.distanceLabel.text = [NSString stringWithFormat:#"%#km",[item objectForKey:#"distance"]];
the displayed value should only ever have two decimals but for some reason values between 1.00 and 9.99 display with more than two decimals.
Can anyone help me with what I am doing wrong here?
I believe you have to either use:
NSLog(#"THE LOG SCORE : %f", x);
OR you need to convert the float to a string like this:
NSString *myString = [[NSNumber numberWithFloat:myFloat] stringValue];

iOS9 AppleLanguages different from older iOS

How I get now the actual system language? It seems that they put regional suffix after last dash. So before cs is now cs-DE if the language is Czech and regional setting is German. But there are some languages which don't have the suffix like GB language is en-GB but regional setting is German.
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* language = [defs objectForKey:#"AppleLanguages"];
NSString* preferredLang = [language objectAtIndex:0];
NSLog(#"localeIdentifier: %#", preferredLang);
Use the componentsFromLocaleIdentifier method from NSLocale class
Here is the documentation
You can do like this:
NSString* localeID = [NSLocale currentLocale].localeIdentifier;
NSDictionary* components = [NSLocale componentsFromLocaleIdentifier:localeID];
NSString* languageID = components[NSLocaleLanguageCode];
EDIT
Getting the language this way will create some issues if the language the app is currently translated in is not the device's language. Indeed,
components[NSLocaleLanguageCode] will return the device's language.
To get the app's current language, you should use [[NSBundle mainBundle] preferredLocalizations].firstObject.
To get the device's region, you can still use components[NSLocaleCountryCode]
I just run into this problem recently. According to Apple's documentation, you will get the locale id with region designator which for like [language designator]-[region designator] on iOS 9.
I found a solution if you just wanna get the locale id, you could use
[[NSBundle mainBundle] preferredLocalizations].
One more solution, If any of you like,
NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
if ([[currentLanguage componentsSeparatedByString:#"-"] count] == 2)
currentLanguage = [[currentLanguage componentsSeparatedByString:#"-"] objectAtIndex:0];
// Only for chinese Language.
else if ([[currentLanguage componentsSeparatedByString:#"-"] count] == 3)
currentLanguage = [NSString stringWithFormat:#"%#-%#", [[currentLanguage componentsSeparatedByString:#"-"] objectAtIndex:0],
[[currentLanguage componentsSeparatedByString:#"-"] objectAtIndex:1]
];
"currentLanguage" Will give you your current langauge so you can use it for localise or any further use.

How to set version number for SDK in iOS/Objective C?

I'm writing an iOS SDK using Objective-C programming language. I would like to know if there is a field in Xcode where i can set version number for SDK
Is there a way in objective-C to set version and build number the way we do it for iOS apps (EX: Version: 2.5.1 Build: 2.5.1.12) ?
Also need a way to detect this version number so i can expose an API something like
- (NSString *)getSDKVersion {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *majorVersion = [infoDictionary objectForKey:#"CFBundleShortVersionString"];
NSString *minorVersion = [infoDictionary objectForKey:#"CFBundleVersion"];
return [NSString stringWithFormat:#"SDK Version %# (%#)", majorVersion, minorVersion];
}
-(NSString*)getSDKBuildVersion;
Which returns the SDK version and build number.
Using:
Xcode - 7.0 (Beta 3 version)
Thanks in advance.
You can set version and build number clicking in the Project(left side) -> General tab(right side) -> Identity section. You will find the fields: Bundle Identifier, Version and Build.
For get the values programmatically:
NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];
NSString *version = [infoDictionary objectForKey:#"CFBundleShortVersionString"];
NSString *build = [infoDictionary objectForKey:kCFBundleVersionKey];

currentInputMode alternative for IOS 7

as currentInputMode is Deprecated in iOS 7.0
so what to use to get current input language
i tired with activeInputModes but that does not change the order
NSArray *currentar = [UITextInputMode activeInputModes];
UITextInputMode *current = [currentar firstObject];
NSString *primaryLanguage = current.primaryLanguage;
NSLog(#"Current text input is: %#", primaryLanguage);
Try this: yourTextView.textInputMode.primaryLanguage;

Convert float to NSString using custom format? ("xx:yy")

I'm getting a JSON string back from a web service I'm using, one of the items is a float, which is formatted like this: "1.2".
But I actually want to make it show like a time number, so like this: "01:20".
What would be the easiest way of doing this?
I thought about converting the float to a string and then splitting it into 2 pieces
timeValue = [NSString stringWithFormat:#"%.2f", timeValue];
NSArray *tmpArr = [timeValue componentsSeparatedByString:#"."];
NSString *tmpFirst = (NSString *)[tmpArr objectAtIndex:0];
NSString *tmpSecond = (NSString *)[tmpArr objectAtIndex:0];
But somehow when I convert it, it returns me a negative number
NSLog(#"timeValue: %#", timeValue);
timeValue: -1.99
I think the problem is in this line.
timeValue = [NSString stringWithFormat:#"%.2f", timeValue];
I think timeValue is of type NSString if not then why are you using same variable twice.
It should be like
timeValueString = [NSString stringWithFormat:#"%.2f", timeValueFloat];

Resources