programing

어떤 iOS 버전이 실행 중인지 프로그래밍 방식으로 감지하려면 어떻게 해야 합니까?

showcode 2023. 4. 20. 23:25
반응형

어떤 iOS 버전이 실행 중인지 프로그래밍 방식으로 감지하려면 어떻게 해야 합니까?

5.0 이하 iOS에서 앱을 실행하고 있는지 확인하고 앱에 라벨을 표시하고 싶습니다.

사용자의 기기에서 실행 중인 iOS를 프로그래밍 방식으로 감지하려면 어떻게 해야 합니까?

감사합니다!

NSString 내에서 숫자 검색을 처리할 필요가 없는 최고의 최신 버전은 다음을 정의하는 것입니다.macros(원래 답변 참조:iPhone iOS 버전 확인)

이러한 매크로는 github에 존재합니다.https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h 를 참조해 주세요.

다음과 같이 합니다.

#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)

다음과 같이 사용합니다.

if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
    // code here
}

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
    // code here
}

아래 구식 버전

OS 버전을 가져오려면:

[[UIDevice currentDevice] systemVersion]

문자열을 반환합니다.이 문자열은 를 통해 int/interface로 변환할 수 있습니다.

-[NSString floatValue]
-[NSString intValue]

이것처럼.

두 값(floatValue, intValue)은 유형에 따라 제거되며 5.0.1은 5.0 또는 5(float 또는 int)가 됩니다.정확하게 비교하려면 다음 INTs check accepted answer 배열과 분리해야 합니다.iPhone iOS 버전

NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];

이렇게 비교하고

NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;

Swift 4.0 구문용

다음 예시는 디바이스가 다음 중 하나인지 확인하는 것입니다.iOS11또는 그 이상의 버전.

let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
    //"for ios 11"
  }
else{
   //"ios below 11")
  }

갱신하다

iOS 8부터 새로운 기능을 사용할 수 있습니다.isOperatingSystemAtLeastVersion에 대한 방법.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
   }

iOS 8 이전에는 API가 존재하지 않았기 때문에 iOS 7에서 크래쉬가 발생할 수 있습니다. iOS 7 이하를 지원하는 경우, 이 체크는 안전하게 수행할 수 있습니다.

if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
  // conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
  // we're on iOS 7 or below
}

오리지널 답변 iOS < 8

완성도를 높이기 위해 iOS 7 UI 전환 가이드에서 Apple이 제안한 대체 접근 방식을 소개합니다. 이 가이드에는 Foundation Framework 버전을 확인하는 작업이 포함됩니다.

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
   // Load resources for iOS 6.1 or earlier
} else {
   // Load resources for iOS 7 or later
}

이 질문에 답하기엔 너무 늦은 거 알아.낮은 iOS 버전(< 5.0):

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);

다음과 같은 결과를 얻을 수 있습니다.

[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
[[UIDevice currentDevice] systemVersion]

[[UIDevice currentDevice] systemVersion];

또는 다음과 같은 버전을 확인합니다.

아래의 매크로는 이쪽에서 입수할 수 있습니다.

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;

}

도움이 되었으면 좋겠다

[[[UIDevice currentDevice] systemVersion] floatValue]

대부분의 경우 Marrek Sebera는 훌륭하지만, 저처럼 iOS 버전을 자주 확인할 필요가 있다면 메모리에서 매크로를 계속 실행하는 것은 원치 않을 것입니다. 왜냐하면 특히 오래된 디바이스에서는 속도가 매우 느려지기 때문입니다.

대신 iOS 버전을 플로트로 한 번 계산한 후 어딘가에 저장해야 합니다.저 같은 경우에는...GlobalVariables싱글톤 클래스는 다음과 같은 코드를 사용하여 코드의 iOS 버전을 확인하는 데 사용합니다.

if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
    // do something if iOS is 6.0 or greater
}

앱에서 이 기능을 활성화하려면 다음 코드를 사용하십시오(ARC를 사용하는 iOS 5+의 경우).

Global Variables(글로벌 변수)h:

@interface GlobalVariables : NSObject

@property (nonatomic) CGFloat iOSVersion;

    + (GlobalVariables *)sharedVariables;

@end

Global Variables(글로벌 변수)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

매크로를 계속 실행하지 않고도 iOS 버전을 쉽게 확인할 수 있습니다.해 주세요.[[UIDevice currentDevice] systemVersion]이 페이지에서 이미 지적된 부적절한 방법을 사용하지 않고 항상 액세스할 수 있는 CGFloat에 대한 NSString.이 방법에서는 버전 문자열이 n.nn.nn 형식(나중의 비트가 누락될 수 있음)으로 iOS5+에서 동작하는 것을 전제로 하고 있습니다.테스트에서 이 접근 방식은 매크로를 지속적으로 실행하는 것보다 훨씬 빠르게 실행됩니다.

제가 겪은 문제를 겪고 있는 모든 사람에게 도움이 되길 바랍니다!

MonoTouch의 경우:

줄자 버전을 가져오려면 다음을 사용하십시오.

UIDevice.CurrentDevice.SystemVersion.Split('.')[0]

마이너 버전 사용:

UIDevice.CurrentDevice.SystemVersion.Split('.')[1]

메이저 버전과 마이너버전을 구분하여 보다 구체적인 버전 번호 정보를 얻으려면 다음 절차를 따릅니다.

NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:@"."];

" " "vN는 줄자버전과 마이너버전을 문자열로 포함하지만 비교하려면 버전 번호를 숫자(ints)로 저장해야 합니다.이 코드를 추가하여 C-array*에 저장할 수 있습니다.versionNumbers:

int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
    versionNumbers[i] = [[vN objectAtIndex:i] integerValue];

* 여기서 사용하는 C-array는 보다 간결한 구문입니다.

iOS 버전이 5 미만인지 간단하게 확인합니다(모든 버전).

if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
        // do something
};

언급URL : https://stackoverflow.com/questions/7848766/how-can-we-programmatically-detect-which-ios-version-is-device-running-on

반응형