facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.27k stars 24.35k forks source link

canOpenURL and openURL return success on androidTV even if app not installed #35793

Closed OrenMe closed 9 months ago

OrenMe commented 1 year ago

Description

When trying to check of a universal link is supported or can be open on AndroidTV the call to canOpenURL always returns true, no matter if there is an app to open it or not and calling openURL doesn't fail even if there is no app to support it and a notification appears at bottom of screen saying "you don't have an app that can do this"

I would expect it to fail on universal links as there is no browser to open these urls and only if there is an app that registers them and support them it will return true. The use case is that I want to try and open an app with its supported universal link and if not supported cause it is not installed so direct to the store.

Version

0.70.6

Output of npx react-native info

System: OS: macOS 13.0.1 CPU: (10) arm64 Apple M1 Pro Memory: 76.34 MB / 32.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node Yarn: 1.22.19 - ~/.yarn/bin/yarn npm: 8.10.0 - ~/.nvm/versions/node/v16.14.0/bin/npm Watchman: 2022.07.04.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 22.1, iOS 16.1, macOS 13.0, tvOS 16.1, watchOS 9.1 Android SDK: API Levels: 28, 29, 30, 31, 32, 33 Build Tools: 30.0.2, 30.0.3, 32.0.0, 32.1.0, 33.0.0 System Images: android-23 | Android TV ARM EABI v7a, android-30 | Android TV Intel x86 Atom, android-30 | ARM 64 v8a, android-30 | Intel x86 Atom_64, android-30 | Google TV Intel x86 Atom, android-30 | Google APIs ARM 64 v8a, android-31 | Android TV ARM 64 v8a, android-31 | Android TV Intel x86 Atom, android-32 | Google APIs ARM 64 v8a, android-33 | Android TV ARM 64 v8a, android-33 | Google APIs ARM 64 v8a Android NDK: Not Found IDEs: Android Studio: 2021.2 AI-212.5712.43.2112.8609683 Xcode: 14.1/14B47b - /usr/bin/xcodebuild Languages: Java: 11.0.11 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.1.0 => 18.1.0 react-native: 0.70.6 => 0.70.6 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

  1. create app from react-native-cli npx react-native init testLinking
  2. cd into directory created cd testLinking
  3. start metro bundler npx react-native start
  4. add to test app the following code under App
    
    Linking.canOpenURL('https://google.com')
      .then(x => console.info('canOpenURL success', x))
      .catch(e => console.info('canOpenURL failed', e))

Linking.openURL('https://google.com') .then(x => console.info('openURL success', x)) .catch(e => console.info('openURL failed', e))

5. start project on android TV (emulator or real device) `npx react-native run-android`
6. See that the results of calls are that link can open and that openURL doesn't catch but no app is there to support it and a notification is opened in the bottom of screen saying "you don't have an app that can do this"

### Snack, code example, screenshot, or link to a repository

```javascript
const App: () => Node = () => {
  const isDarkMode = useColorScheme() === 'dark';
  const [canOpenUrlRes, setCanOpenUrlRes] = useState(null)
  const [openUrlRes, setOpenUrlRes] = useState(null)
  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  Linking.canOpenURL('https://google.com') //this url is just an example, any http/https url will return true
    .then(x=>{setCanOpenUrlRes(x); console.info('canOpenURL success', x)})
    .catch(x=>{setCanOpenUrlRes(false); console.info('canOpenURL fail', x)})
  Linking.openURL('https://google.com') //this url is just an example, any http/https url will return true
    .then(x=>{setOpenUrlRes(x); console.info('openURL success', x)})
    .catch(x=>{setOpenUrlRes(false); console.info('openURL fail', x)}) 

  return (
    <SafeAreaView style={backgroundStyle}>
      <StatusBar
        barStyle={isDarkMode ? 'light-content' : 'dark-content'}
        backgroundColor={backgroundStyle.backgroundColor}
      />
      <ScrollView
        contentInsetAdjustmentBehavior="automatic"
        style={backgroundStyle}>
        <View
          style={{
            backgroundColor: isDarkMode ? Colors.black : Colors.white,
          }}>
          <Section title="test openURL and canOpenURL on https://google.com"></Section>
          <Section title="canOpenURL">
            <Text style={styles.highlight}>can open URL results: {canOpenUrlRes? 'true': 'false'}</Text>        
          </Section>
          <Section title="openURL">
            <Text style={styles.highlight}>open URL results: {openUrlRes? 'true': 'false'}</Text>
          </Section>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};
OrenMe commented 1 year ago

any update on this? is this expected?

rawatnaresh commented 1 year ago

Maybe this can be helpful. https://developer.android.com/training/package-visibility/declaring#package-name

Basically you have to declare your package visibility as well. Otherwise canOpenUrl always returns true.


<manifest>
    <queries>
        <package android:name="com.example.store" /> <-- package name of the app that you want to access-->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="scheme"/>
        </intent>
    </queries>
</manifest>
MitchellCoutinho commented 1 year ago

Unsure if this is related, but it also always appears to return true for Android App Links on API levels 28, 29 and 30.

github-actions[bot] commented 9 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 9 months ago

This issue was closed because it has been stalled for 7 days with no activity.

mashwishi commented 7 months ago

any update on this?