Getting mixed results trying to open a specific page in social media apps/sites with Linking, WebBrowser

  1. SDK Version: 40
  2. Platforms(Android/iOS/web/all): Android and iOS

Hello. I’m using the code extract below to open a specific page/id/username on Facebook, Instagram, and Twitter - first trying the app with the relevant scheme URL, and then the website if it fails.

It’s ‘kind of’ working on both Android and iOS - but sometimes the social media app doesn’t open even if it’s installed and the user is logged in, and the website opens instead, and often Facebook goes to the user’s own timeline rather than the specific page I want.

The three scheme URLs I’m using are:

fb://page?id=000000000000000
instagram://user?username=USERNAME
twitter://user?id=00000000

Am I doing anything obviously wrong?

import * as Linking from "expo-linking";
import * as WebBrowser from "expo-web-browser";

const handleAppOrWeb = async (appURL, webURL) => {
		try {
			const isApp = await Linking.canOpenURL(appURL);
			console.log(isApp);
			if (isApp) {
				await Linking.openURL(appURL);
			} else {
				await WebBrowser.openBrowserAsync(webURL);
			}
		} catch (error) {
			console.error(error);
		}
	};

I’ve got the following in my app.json file for iOS (nothing for Android).

"infoPlist": {
				"UIBackgroundModes": ["audio"],
				"LSApplicationQueriesSchemes": ["fb", "instagram", "twitter"]
			}

Okay, I’ve fixed everything.

First, the apps weren’t opening on one particular iPhone because it’s old and the iOS can’t run TestFlight. So the phone was running my app through Expo, and of course linking to apps doesn’t work in Expo - so I was getting the websites instead.

Second, Facebook has two fb:// URL formats for linking to specific pages - one for iOS and a different one for Android. I was just using the iOS one.

Here are my updated code snippets.

const handleAppOrWeb = async (appURL, webURL) => {
		try {
			const isApp = await Linking.canOpenURL(appURL);
			isApp ? Linking.openURL(appURL) : WebBrowser.openBrowserAsync(webURL);
		} catch (error) {
			console.error(error);
		}
	};

const facebookURL =
	Platform.OS === "ios"
	? "fb://page?id=000000000000000"
	: "fb://page/000000000000000";

SDK Version: 40 Platforms(Android/iOS/web/all): Android and iOS Hello. I’m using the code extract Getting mixed results trying to open a specific page in social media apps / sites with Linking, WebBrowser · Help: Expo SDK.

Thanks for the help

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.