luisfcofv/react-native-deep-linking

[ Question ] How make copy file from email attachment work when app closed?

esutton opened this issue · 4 comments

It works great if iOS app has been launched previously. App comes to foreground and open url event is called.

If app is not launched, the app will launch, however, the open url event never seems to get called?

Thanks in advance for any tips or suggestions.

Hi @esutton, did you add the getInitialURL() method to your app?
This method is called when a deep link launches the app.

componentDidMount() {
  Linking.getInitialURL().then((url) => {
    if (url) {
      Linking.openURL(url);
    }
  }).catch(err => console.error('An error occurred', err));
}

If yes, is componentDidMount being called?
This might also be a misconfiguration in your AppDelegate.m

I'm updating the example to handle this case.

Hi @luisfcofv, no I did not use getInitialURL()

All I had was.

  componentDidMount() {
    Linking.addEventListener('url', this._handleOpenURL);
  },

I will try getInitialURL()

I can see openURL being called in AppDelegate.m. I can copy email attachments to iOS app if app had been launched previously.

Thanks again!

Works great! Thank you @luisfcofv

  componentDidMount() {
    Linking.getInitialURL()
      .then(url => {
        if (url) {
            const event = {};
            event.url = url;
            this._handleOpenURL(event);
        }
      })
      .catch(err => console.error('An error occurred', err));

    Linking.addEventListener('url', this._handleOpenURL);
  },

I don't think your timeout is necessary.

One tip, no need to call this._handleOpenURL inside getInitialURL()
You can have your function as:

import { Linking } from 'react-native'
...
componentDidMount() {
  Linking.addEventListener('url', this._handleOpenURL);
  Linking.getInitialURL()
    .then(url => {
      if (url) {
        Linking.openURL(url);
      }
    })
    .catch(err => console.error('An error occurred', err));
}

Closing this issue :)