Platform-Specific Code — Mobile

React Native code is mostly shared, and the parts that differ are written with Platform.OS ('ios' or 'android'), Platform.select({ ios, android, native…

You give the order card a light shadow with the shadow* properties. On an iPhone the shadow shows, on an Android phone the card is flat. Same code, different platform. That's what platform-specific code is for: the small part of your code that is deliberately different for iOS and Android, while the rest stays shared. It's like Mrs. Rini's second branch in a mall. The menu, the prices and the way it works are exactly the same as at the shophouse. Only what the building decides is different: a sliding door at the mall, a push door at the shophouse. Mrs. Rini doesn't write a second manual, just one page "for the mall branch only". Mrs. Rini's Laundry app is the same: nearly all of its code is shared, and only a few lines know where they are running.

Platform.OS and Platform.select

import { Platform } from 'react-native';

Platform.OS;      // 'ios' or 'android' ('web' when it runs in a browser)
Platform.Version; // Android: the API level as a number, e.g. 35. iOS: a string, e.g. '18.2'

const needsNotifPermission = Platform.OS === 'android' && Platform.Version >= 33; // Android 13 and up
const linkColor = Platform.select({ ios: '#007aff', android: '#1a73e8', default: '#2563eb' });

.ios.tsx and .android.tsx files

When the implementation is completely different, split it by file. Example: the "Open in maps" button for the pickup address.

// src/lib/openMaps.ios.ts: Apple Maps
import { Linking } from 'react-native';
export function openMaps(address: string) {
  return Linking.openURL(`https://maps.apple.com/?q=${encodeURIComponent(address)}`);
}

// src/lib/openMaps.android.ts: Android's built-in maps app
import { Linking } from 'react-native';
export function openMaps(address: string) {
  return Linking.openURL(`geo:0,0?q=${encodeURIComponent(address)}`);
}

// src/lib/openMaps.ts: the fallback for web, and the file TypeScript reads
import { Linking } from 'react-native';
export function openMaps(address: string) {
  return Linking.openURL(`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(address)}`);
}

Every screen just writes import { openMaps } from '@/lib/openMaps'. Metro picks the file when it builds the bundle: for iOS it looks for .ios.ts, then .native.ts, then .ts. Checked through Metro on an Expo SDK 57 project: the iOS bundle contains only maps.apple.com, the Android bundle only geo:0,0, and the web bundle only google.com/maps. The Expo template itself does this, for example app-tabs.web.tsx. The .ts file with no suffix is still needed, because TypeScript knows nothing about platforms and looks for that file.

The choice: a few values that differ, use Platform.select. A whole function or component that differs, use .ios and .android files.

Shadows and fonts

const styles = StyleSheet.create({
  card: {
    padding: 16,
    borderRadius: 12,
    backgroundColor: '#fff',
    ...Platform.select({
      ios: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.15, shadowRadius: 6 },
      android: { elevation: 3 },
    }),
  },
  // or one line that is the same on both platforms:
  newCard: { boxShadow: '0 2px 6px rgba(0, 0, 0, 0.15)' },
});
Style iOS Android
shadowColor, shadowOffset, shadowOpacity, shadowRadius a shadow only shadowColor is used (Android 9 and up), to tint the elevation shadow
elevation: 3 no effect Android's own shadow
boxShadow a shadow a shadow (Android 9 and up)

boxShadow is written like CSS and arrived in React Native 0.76, for the New Architecture only. Since 0.82 the New Architecture is the only one, so on Expo SDK 57 it works on both platforms.

Fonts differ too. Without fontFamily, iOS uses San Francisco and Android uses Roboto. Their letters aren't the same width, so the same text can be longer on one of the phones. That's one more reason to give flex: 1 to the part that may narrow.

Back: Android's button and iOS's swipe

Android has a system back (a button or a gesture). iOS doesn't: you go back with the button in the header or by swiping from the left edge. BackHandler only hears Android's system back.

// src/app/pickup.tsx
import { router, Stack, useFocusEffect } from 'expo-router';
import { useCallback, useState } from 'react';
import { Alert, BackHandler, TextInput, View } from 'react-native';

export default function RequestPickup() {
  const [note, setNote] = useState('');
  const hasNote = note.length > 0;

  useFocusEffect(
    useCallback(() => {
      const sub = BackHandler.addEventListener('hardwareBackPress', () => {
        if (!hasNote) return false; // let navigation close the screen
        Alert.alert('Cancel the pickup request?', 'The unsent note will be lost.', [
          { text: 'Keep writing', style: 'cancel' },
          { text: 'Discard', style: 'destructive', onPress: () => router.back() },
        ]);
        return true; // handled: the screen stays open
      });
      return () => sub.remove();
    }, [hasNote]),
  );

  return (
    <View style={{ flex: 1, padding: 16 }}>
      <Stack.Screen options={{ title: 'Request pickup', gestureEnabled: !hasNote }} />
      <TextInput value={note} onChangeText={setNote} placeholder="Note for the courier" />
    </View>
  );
}

On an iPhone, the swipe from the left edge is handled by native navigation, not by BackHandler. So without gestureEnabled: false, the screen just closes and the note is lost with no question asked. The back button in the header isn't the system back either; navigation has a beforeRemove event to catch every way out at once.

Status bar and permissions

Wulan taps "Don't allow" iOS Android
The first time the dialog will never show again, canAskAgain: false the dialog may still show once more
The second time (the dialog no longer shows) since Android 11, the dialog no longer shows, canAskAgain: false
// src/components/ReceiptPhoto.tsx, the component's body
const [permission, requestPermission] = useCameraPermissions(); // from expo-camera
if (!permission) return null; // permission status not read yet
if (permission.granted) return <Text>Camera ready.</Text>;
if (!permission.canAskAgain) return <Button title="Open Settings" onPress={() => Linking.openSettings()} />;
return <Button title="Allow the camera to photograph receipts" onPress={requestPermission} />;

On iOS, every permission also needs a reason sentence in Info.plist (for example NSCameraUsageDescription). Notifications on Android 13 (API 33) and up need a permission too, which is why needsNotifPermission exists above.

What Expo modules hide: expo-status-bar and useCameraPermissions give one API for two platforms, and config plugins write Info.plist and AndroidManifest.xml for you (the next lesson). What they don't hide: how you go back, the permission rules, shadows, fonts, and each platform's interface habits (Material Design on Android, the Human Interface Guidelines on iOS).

⚠️ Platform differences that slip through

TL;DR: React Native code is mostly shared, and the parts that differ are written with Platform.OS ('ios' or 'android'), Platform.select({ ios, android, native, default }), or .ios.tsx and .android.tsx files that Metro picks at bundle time (plus a file with no suffix for web and TypeScript). Shadows: shadow* on iOS, elevation on Android, or boxShadow for both (since 0.76, New Architecture). BackHandler is only for Android's system back (return true stops it); the iOS swipe from the left edge is turned off with gestureEnabled: false. expo-status-bar with style="auto" handles the status bar text colour. Permissions: iOS asks only once, Android twice since Android 11; after that canAskAgain: false and all that's left is Linking.openSettings().

⚡ One value per platform

Write:

const linkColor = Platform.select({ ios: '#007aff', android: '#1a73e8', default: '#2563eb' });

Meaning: Platform.select picks a value by platform: that platform's key first (ios or android), then native, then default. For a one- or two-line if, Platform.OS is enough.

⚡ One file per platform

Write:

import { openMaps } from '@/lib/openMaps';

Meaning: There are openMaps.ios.ts, openMaps.android.ts and openMaps.ts. Metro picks the file when it builds the bundle, so the iOS bundle holds only the Apple Maps version. The file with no suffix is still needed for web and TypeScript.

⚡ The missing shadow

Guess first: the order card only gets shadowColor, shadowOffset, shadowOpacity and shadowRadius. How does it look on an Android phone?

Flat, with no shadow. The shadow* properties are for iOS, while Android uses elevation.

Meaning: Write both through Platform.select, or one boxShadow line that works on both platforms since React Native 0.76.

What you will learn