luisfcofv / react-native-deep-linking

Simple route-matching library to handle deep links
MIT License
247 stars 23 forks source link

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

Closed esutton closed 7 years ago

esutton commented 7 years ago

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.

luisfcofv commented 7 years ago

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.

esutton commented 7 years ago

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!

esutton commented 7 years ago

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);
  },
luisfcofv commented 7 years ago

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 :)