Linking.getInitialURL() is not being cleared after used for deeplink

前端 未结 2 1793
遥遥无期
遥遥无期 2021-01-21 17:27

I\'ve had this problem for like 2 weeks. I used Wix\'s Navigation for navigating around the app. I followed this tutorial for implementing the deeplink/universal link.

I

相关标签:
2条回答
  • 2021-01-21 17:49

    There are two ways to handle URLs that open your app.

    1. If the app is already open, the app is foregrounded and a Linking event is fired You can handle these events with Linking.addEventListener(url, callback).

    2. If the app is not already open, it is opened and the url is passed in as the initialURL You can handle these events with Linking.getInitialURL(url) -- it returns a Promise that resolves to the url, if there is one.

    You can read more detail Here is the example

    export default class App extends Component {
      constructor(props) {
        super(props)
        this.state = {
          initialised: false
        }
      }
    
      componentDidMount() {
        AppState.addEventListener('change', this._handleAppStateChange);
        Linking.addEventListener('url', event => {
           console.log('deep link from background', event.url)
        })
      }
    
      _handleAppStateChange = async (nextAppState) => {
        const url = await Linking.getInitialURL();
        if (url !== null && !this.state.initialised) {
          this.setState({ initialised: true })
          console.log('deep link from init app', url)
        }
      }
    
      componentWillUnmount() {
        AppState.removeEventListener('change', this._handleAppStateChange);
        Linking.removeEventListener('url')
      }
    }
    
    0 讨论(0)
  • 2021-01-21 18:03

    Linking.getInitialURL() gives us the same Url when we come back to the same page again, to Overcome this we can do a simple condition of not to call the DeepLink function. Something like...

    Step 1: First init a dummyDeepLinkedUrl String .

    var dummyDeepLinkedUrl;
    

    Step 2: Check for the condition like, if deeplinkUrl is coming from Linking.getInitialURL() and deeplinkUrl is not equal to the dummyDeepLinkedUrl .

    if (url && url != dummyDeepLinkedUrl) {}
    

    Step 3: If not same call the Deeplink Function and assign the deeplinkUrl to dummyDeepLinkedUrl.

        this.navigateToRespectivePage(url);
        dummyDeepLinkedUrl = url;
    

    Finally this will look like :

    Linking.getInitialURL().then(url => {
          if (url && url != dummyDeepLinkedUrl) {
            this.navigateToRespectivePage(url);
            dummyDeepLinkedUrl = url;
          }
        });
    
    0 讨论(0)
提交回复
热议问题