How to get iOS version in React Native?
Asked Answered
C

3

16

I'm facing the difficulty to retrieve iOS version and according to this link https://github.com/facebook/react-native/issues/3766 , it seems there isn't gonna be any support to retrieve iOS Version. I've tried third partyprovider but seems like unable to retrieve iOS Version too.

May I know how you guys handle that?

Caenogenesis answered 17/9, 2018 at 6:14 Comment(0)
L
34

Detecting the iOS version

On iOS, the Version is a result of -[UIDevice systemVersion], which is a string with the current version of the operating system. An example of the system version is "10.3". For example, to detect the major version number on iOS:

import {Platform} from 'react-native';

const majorVersionIOS = parseInt(Platform.Version, 10);
console.log("iOS version " + majorVersionIOS);
Lysol answered 17/9, 2018 at 6:28 Comment(0)
S
2

This is my DeviceUtils.ts file to reuse that logic. This could be greatly improved, happy to hear suggestions.

  • Get Major Version
export const getMajorVersionIOS = (): number => {
  if (Platform.OS != 'ios' ) {
    throw Error("Platform is not iOS");
  }

  return parseInt(Platform.Version, 10);
}
  • Get Minor Version
export const getMinorVersionIOS = (): number => {
  if (Platform.OS != 'ios' ) {
    throw Error("Platform is not iOS");
  }

  return parseInt(
    Platform.Version.substring(
      Platform.Version.indexOf('.') + 1
    )
  , 10);
}
  • If it's iOS >= 14.5
export const isIOSAbove14Point5 = (): boolean => {
  if (Platform.OS === 'ios' ) {
    const majorVersion = getMajorVersionIOS();
    const minorVersion = getMinorVersionIOS();

    if (majorVersion >= 15) return true;

    if (majorVersion === 14 && minorVersion >= 5) return true;
  }

  // It's not iOS or it's not above 14.5
  return false;
}
Showalter answered 16/7, 2022 at 5:16 Comment(0)
J
-1
export const isPlatformVersionHigher = (
  majorVersion: number,
  minorVersion: number = 0
) => {
  // On android Platform is API number
  if (typeof Platform.Version === 'number' && Platform.OS === 'android') {
    return Platform.Version >= majorVersion;
  }

  if (Platform.OS === 'ios') {
    const [major, minor] = (Platform.Version as string)
      .split('.')
      .map((c) => parseInt(c, 10));

    if (!major || !minor) {
      return false;
    }

    return majorVersion >= major && minorVersion >= minor;
  }

  return false;
};

This is a clean and dirty way to check platform versions are higher than the given major and minor versions respectively.

On android Platform.Version will always return a number.

Julide answered 26/12, 2022 at 11:5 Comment(1)
The iOS code will fail on cases where the minor version differs. You probably need to do multiple minor checks. And, the iOS code seems flipped. I.e. return majorVersion < major || (majorVersion === major && minorVersion < minor)Rauscher

© 2022 - 2024 — McMap. All rights reserved.