Jak możemy programowo wykryć, na której wersji systemu iOS działa urządzenie? [duplikat]
To pytanie ma już odpowiedź tutaj:
- Jak sprawdzić wersję iOS? 38 odpowiedzi
Chcę sprawdzić, czy użytkownik jest uruchomiony na iOS mniej niż 5.0 i wyświetlić etykietę w aplikacji.
Jak wykryć programowo, który iOS działa na urządzeniu użytkownika?
Dzięki!
10 answers
Najlepsza aktualna wersja, bez konieczności radzenia sobie z numerycznym wyszukiwaniem w NSString jest zdefiniowanie macros
(Zobacz oryginalną odpowiedź: Sprawdź wersję iPhone iOS)
Te makra istnieją w GitHubie, zobacz: https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
Tak:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
I używaj ich w ten sposób:
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
// code here
}
Przestarzała wersja poniżej
Aby pobrać wersję systemu operacyjnego:
[[UIDevice currentDevice] systemVersion]
Zwraca łańcuch, który można przekształcić w int / float poprzez
-[NSString floatValue]
-[NSString intValue]
Like this
Obie wartości (floatValue, intValue) zostaną usunięte ze względu na swój typ, 5.0.1 stanie się 5.0 lub 5 (float lub int), dla dokładnego porównania, będziesz musiał oddzielić je do tablicy INTs sprawdź akceptowaną odpowiedź tutaj: Sprawdź wersję iPhone iOS
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];
I porównaj tak
NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;
Dla składni Swift 4.0
Poniższy przykład to tylko sprawdzenie czy urządzenie jest iOS11
lub większa wersja.
let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
//"for ios 11"
}
else{
//"ios below 11")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-06 13:24:08
Update
Z iOS 8 możemy użyć nowej metody isOperatingSystemAtLeastVersion
na NSProcessInfo
NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
// iOS 8.0.1 and above logic
} else {
// iOS 8.0.0 and below logic
}
Uważaj, że spowoduje to awarię na iOS 7, ponieważ API nie istniało przed iOS 8. Jeśli obsługujesz system iOS 7 i nowszy, możesz bezpiecznie wykonać kontrolę za pomocą
if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
// conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
// we're on iOS 7 or below
}
Oryginalna odpowiedź
Aby uzyskać kompletność, oto alternatywne podejście zaproponowane przez samego Apple w iOS 7 UI Transition Guide , które obejmuje sprawdzenie frameworka Foundation wersja.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-05 06:25:25
Wiem, że jestem za późno, by odpowiedzieć na to pytanie. Nie jestem pewien, czy moja metoda nadal działa na niskich wersjach iOS (
NSString *platform = [UIDevice currentDevice].model;
NSLog(@"[UIDevice currentDevice].model: %@",platform);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
Możesz uzyskać te wyniki:
[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-14 07:05:13
[[UIDevice currentDevice] systemVersion]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-21 11:32:16
[[UIDevice currentDevice] systemVersion];
Lub sprawdź wersję jak
Możesz pobrać poniższe makra z tutaj .
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))
{
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cs_lines_back.png"]] autorelease];
theTableView.backgroundView = background;
}
Hope this helps
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:47
[[[UIDevice currentDevice] systemVersion] floatValue]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 06:58:40
Marek Sebera jest świetny przez większość czasu, ale jeśli jesteś taki jak ja i stwierdzisz, że musisz często sprawdzać wersję iOS, nie chcesz stale uruchamiać makra w pamięci, ponieważ doświadczysz bardzo lekkiego spowolnienia, szczególnie na starszych urządzeniach.
Zamiast tego, chcesz obliczyć wersję iOS jako float raz i przechowywać ją gdzieś. W moim przypadku mam klasę GlobalVariables
singleton, której używam do sprawdzania wersji iOS w moim kodzie używając kodu takiego:
if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
// do something if iOS is 6.0 or greater
}
Aby włączyć ta funkcjonalność w aplikacji, Użyj tego kodu (dla iOS 5 + za pomocą ARC):
GlobalVariables.h :
@interface GlobalVariables : NSObject
@property (nonatomic) CGFloat iOSVersion;
+ (GlobalVariables *)sharedVariables;
@end
GlobalVariables.m :
@implementation GlobalVariables
@synthesize iOSVersion;
+ (GlobalVariables *)sharedVariables {
// set up the global variables as a static object
static GlobalVariables *globalVariables = nil;
// check if global variables exist
if (globalVariables == nil) {
// if no, create the global variables class
globalVariables = [[GlobalVariables alloc] init];
// get system version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
// separate system version by periods
NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:@"."];
// set ios version
globalVariables.iOSVersion = [[NSString stringWithFormat:@"%01d.%02d%02d", \
systemVersionComponents.count < 1 ? 0 : \
[[systemVersionComponents objectAtIndex:0] integerValue], \
systemVersionComponents.count < 2 ? 0 : \
[[systemVersionComponents objectAtIndex:1] integerValue], \
systemVersionComponents.count < 3 ? 0 : \
[[systemVersionComponents objectAtIndex:2] integerValue] \
] floatValue];
}
// return singleton instance
return globalVariables;
}
@end
Teraz możesz łatwo sprawdzić wersję iOS bez ciągłego uruchamiania makr. Zauważ w szczególności, jak przekonwertowałem [[UIDevice currentDevice] systemVersion]
NSString na CGFloat, który jest stale dostępny bez użycia żadnej z niewłaściwych metod, które wiele już wskazało na tej stronie. Moje podejście zakłada, że ciąg wersji jest w format NN.nn (pozwalający na brak późniejszych bitów) i działa dla iOS5+. W testach takie podejście działa znacznie szybciej niż ciągłe uruchamianie makra.
Mam nadzieję, że to pomoże każdemu doświadczającemu problemu, który miałem!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-06 22:33:18
Aby uzyskać bardziej szczegółowe informacje o numerze wersji z rozdzielonymi wersjami major i minor:
NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:@"."];
Tablica vN
będzie zawierała wersje major i minor jako ciągi znaków, ale jeśli chcesz dokonać porównania, numery wersji powinny być przechowywane jako liczby (ints). Możesz dodać ten kod, aby zapisać je w tablicy C* versionNumbers
:
int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
versionNumbers[i] = [[vN objectAtIndex:i] integerValue];
* C-tablice używane tutaj dla bardziej zwięzłej składni.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 19:10:49
W MonoTouch:
Aby uzyskać główną wersję użyj:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
Do użycia w wersji minor:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-16 08:41:55
Proste sprawdzenie dla wersji iOS mniejszej niż 5 (Wszystkie wersje):
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-14 14:22:23