14

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);
        });
1110
  • 7,829
  • 55
  • 176
  • 334
  • When you use `return` it also breaks out of particular function you are currently running. So in your upper example it would only trigger first one. I do not see myself any point using return with `Linking.openURL` -method so maybe just leave it out of your code. – Jimi Pajala Oct 16 '18 at 11:30
  • Please prepare a reproduction repository. In your specific issue, anybody can help you just when seeing your sample codes. thanks. – AmerllicA Oct 19 '18 at 13:40

6 Answers6

10

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.

Julien Malige
  • 3,295
  • 1
  • 20
  • 39
9

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');
Akshay I
  • 3,675
  • 1
  • 34
  • 57
5

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.

Jason Gaare
  • 1,511
  • 10
  • 19
  • I get false for supported if I use url “twitter://“ or “instagram://“ (android & ios) can you try those urls in your code please? – 1110 Oct 16 '18 at 19:21
  • Using the url "twitter://" works for me on initial tests on my iOS device. Curious it doesn't work on yours... did you try removing that return statement? – Jason Gaare Oct 16 '18 at 20:14
  • Yes I removed return statement but it works on IOS now but on android don't. Does it work on your android device? If I use "fb://" on IOS facebook is opened but on android nothing happens. – 1110 Oct 17 '18 at 06:59
1

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

Linking.openURL('https://www.facebook.com/');
Shyqa
  • 46
  • 6
0

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.

VAK
  • 21
  • 6
0

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.

Florin Dobre
  • 9,872
  • 3
  • 59
  • 93