In this lesson, you will learn how to write conditional logic that allows your React Native app to tap into unique OS-level services, like FaceID on iOS or specific system settings on Android.
The Core Concept: The Platform Module
Because React Native acts as a bridge between your JavaScript code and the underlying operating system, sometimes you need to ask: "Am I running on iOS or Android?"
React Native provides a built-in utility called Platform that lets you inspect the runtime environment. This is essential when you want to access APIs that only exist on one platform. For example, if you want to trigger a biometric authentication prompt (like FaceID or Android Fingerprint), you cannot use a "one size fits all" command. You must detect the platform and call the specific native service provided by that OS.
How to use Conditional Logic
You can use a simple if statement or the Platform.select helper to swap out functionality. Here is a basic example of how to execute code only on a specific platform:
import { Platform, Alert } from 'react-native';
const checkSystemFeature = () => {
if (Platform.OS === 'ios') {
// Execute logic for iOS-only services
console.log("Accessing iOS Secure Enclave...");
} else if (Platform.OS === 'android') {
// Execute logic for Android-only services
console.log("Accessing Android BiometricPrompt...");
}
};
Why This Matters
When your JavaScript thread talks to the Native thread, it needs to know which native modules are available. If you try to call an iOS-specific API while the app is running on an Android device, the app will crash because that native code doesn't exist in the Android host platform. By using the Platform module, you guard your code, ensuring that you only invoke native services when the host OS is prepared to handle them. This keeps your cross-platform app stable and allows you to leverage the full power of each device’s unique capabilities.
Key Takeaway: Use the Platform module to safely detect the operating system and execute platform-specific code, preventing crashes by only calling services that exist on the current host environment.