1110
1110

Reputation: 6829

Linking in react native can open just one app

UPDATE 1

I removed return from code and now links work on IOS. But on android I can't open any app. Any idea?

I am trying to open different apps from my app.

return Linking.openURL(“twitter://“);
return Linking.openURL(“instagram://“);

But it doesn’t work. I configured IOS by documentation. On android doesn’t work too. While...

return Linking.openURL(“tripadvisor://“);

Work just fine. Any idea why I can’t open other apps.
This is code that I am using (open app if installed or open store with it but sometimes even store doesn't open) what I did wrong:

let appUrl = "instagram://";
Linking.canOpenURL(appUrl).then(supported => {
            if (!supported) {
              Alert.alert("",
                "",
                [
                  {text: "go to store", onPress: this.openStorePress},
                  {text: "cancel", onPress: () => { }, style: 'cancel'},
                ],
                { cancelable: false }
              );
            } else {
              return Linking.openURL(appUrl);
            }
        }).catch(err => {
            console.error(err);
        });

Upvotes: 14

Views: 23036

Answers (6)

Akshay I
Akshay I

Reputation: 4175

You have to find the rights URL schemes. Have look at my code

Instagram

Linking.openURL('instagram://user?username=apple')
  .catch(() => {
    Linking.openURL('https://www.instagram.com/apple');
  })

Twitter

Linking.openURL('twitter://user?screen_name=apple')
  .catch(() => {
    Linking.openURL('https://www.twitter.com/apple');
  })

Facebook

Linking.openURL('fb://page/PAGE_ID');
Linking.openURL('http://instagram.com/_u/USER_NAME');
Linking.openURL('http://instagram.com/_p/PICTURE');

Upvotes: 9

Julien Malige
Julien Malige

Reputation: 3475

Your issue is related to the content of the url, twitter:// means nothing for the Android Twitter app, so it will not open.

For example, the following code should work:

   Linking.openURL('twitter://timeline')

or

    Linking.openURL('instagram://user?username=apple')

You have to find the rights url schemes (documentations are not very clear about it) that may be different between iOS and Android.

Upvotes: 10

Florin Dobre
Florin Dobre

Reputation: 10232

Try to use a package like:

https://github.com/react-native-community/react-native-share

You can try to use only some of it's functions or look into the native code from there and create some bridge functions in the native code and then export them to be used in JS code.

Note: you will have to use real devices for the tests.

Upvotes: 0

Jason Gaare
Jason Gaare

Reputation: 1511

Your code looks pretty solid, here's an example of how I open twitter in my app.

const twitterUrlScheme = `twitter://user?screen_name=${twitterUsername}`;

Linking.canOpenURL(twitterUrlScheme)
    .then((supported) =>
        Linking.openURL(
            supported
                ? twitterUrlScheme
                : `https://www.twitter.com/${twitterUsername}`
            )
        )
        .catch((err) => console.error('An error occurred', err));

I think perhaps your issue might be the return Linking.openUrl, I'm not sure you need the return in that statement. Does it work if you remove the return? Otherwise, it might help to move your Alert outside of the then-block from canOpenUrl.

Upvotes: 5

Shyqa
Shyqa

Reputation: 46

I have used only url and it's working both iOS and android

Linking.openURL('https://www.facebook.com/');

Upvotes: 1

VAK
VAK

Reputation: 21

You haven't completed the " fot twitter and instagram, I don't know whether you made the same mistake in app too, if yes, fixing that might solve it.

Upvotes: 0

Related Questions