React Native course · Module 13: Native APIs

expo-sharing & expo-linking - Sharing and links

1 min read ·
In this lesson3

Share content and open external apps!

expo-sharing

npx expo install expo-sharing
import * as Sharing from 'expo-sharing';

// Check availability
const isAvailable = await Sharing.isAvailableAsync();

// Share a file
async function shareFile(uri: string) {
  if (await Sharing.isAvailableAsync()) {
    await Sharing.shareAsync(uri, {
      mimeType: 'image/jpeg',
      dialogTitle: 'Share a photo from Atlantis',
    });
  }
}

expo-linking

npx expo install expo-linking
import * as Linking from 'expo-linking';

// Open URL
await Linking.openURL('https://atlantis.app');

// Open app settings
await Linking.openSettings();

// Check if URL can be opened
const canOpen = await Linking.canOpenURL('mailto:');

// Deep linking - creating a link to the app
const appUrl = Linking.createURL('treasure', {
  queryParams: { id: '123' },
});
// Result: myapp://treasure?id=123
import * as Linking from 'expo-linking';

function App() {
  const url = Linking.useURL();

  useEffect(() => {
    if (url) {
      const { path, queryParams } = Linking.parse(url);
      // Navigate based on deep link
      if (path === 'treasure' && queryParams?.id) {
        navigation.navigate('TreasureDetail', { id: queryParams.id });
      }
    }
  }, [url]);

  return <AppNavigator />;
}

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you share a file?

  2. 2. How do you open app settings?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Horizontal ordering

    Arrange opening a URL:

  1. QuestionSharing.shareAsync
  2. Put in orderLinking.openURL
  3. QuestionopenSettings
  4. QuestioncreateURL

Useful articles