question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Linking.getInitialURL() url value is null

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

  1. react-native -v: 2.0.1
  2. node -v: 8.4.0
  3. npm -v: 4.6.1

Then, specify:

  • Target Platform: Android
  • Development Operating System: MacOs Sierra
  • Build tools: Application created using create-react-native-app and then detached with support of Expo SDK

Steps to Reproduce

(Write your steps here:)

  1. Create application using create-react-native-app
  2. Detach application with: npm run eject (keep support of Expo SDK)
  3. Update AndroidManifest.xml with respective deep links filters inside MainActivity
<intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https"
              android:host="www.mydomain.com"
              android:pathPrefix="/" />
        <data android:scheme="https"
              android:host="www.mydomain.com" />
</intent-filter>
  1. Update activity declaration with android:launchMode="singleTask"
  2. Update application component to load url on componentDidMount:
componentDidMount() {
    Linking.getInitialURL().then((url) => {
      console.log('Inside of the function is: ' + url);
      this._handleOpenURL(url);
    }).catch(err => console.error('An error occurred', err));
}
  1. Find url is always null

Expected Behavior

url variable populated with respective Url application was loaded with

Actual Behavior

Url is null

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
AuraIVcommented, Nov 28, 2017

Same issue

2reactions
henebbcommented, Dec 29, 2017

Solved it! I’ll explain what the problem for us was, if anyone is facing the same issue as we did.

We had a custom activity called “SplashActivity” where we had added the

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    ...

In our SplashActivity.java we started the MainActivity like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

For RN to pick up the url from getInitialURL(), we need to pass the data and action to our MainActivity. So basically we just passed the required information to our MainActivity (in SplashActivity.java):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri data = null;
    String action = null;
    Intent currentIntent = getIntent();
    if (currentIntent != null) {
        Uri intentData = currentIntent.getData();
        if (intentData != null) {
            data = intentData;
        }

        // Get action as well.
        action = currentIntent.getAction();
    }

    Intent intent = new Intent(this, MainActivity.class);
    // Pass data and action (if available).
    if (data != null) {
        intent.setData(data);
    }
    if (action != null) {
        intent.setAction(action);
    }
    startActivity(intent);
    finish();
}

(don’t forget to import android.content.Intent; and import android.net.Uri;)

This solved it for us.

Read more comments on GitHub >

github_iconTop Results From Across the Web

iOS Linking getInitialURL() always null · Issue #24429 - GitHub
3 it's working via getInitialUrl() and listener. I tried on React Native 0.59.4 not working. To Reproduce. 1. Add URL type to info.plist....
Read more >
Why is Linking.getInitialURL always returning null?
The response was always null if I was debugging remotely. If the remote debugger was off, then I would get the correct url....
Read more >
Native App Deep Link not working | Mendix Forum
getInitialURL() in RegisterDeepLink is returning null value because of this deep link url registration is failing. I am running the App locally ...
Read more >
Linking - Expo Documentation
Returns the initial URL or null . ... Linking.getInitialURL(). Get the URL that was used to launch the app if it was ......
Read more >
Linking · React Native
componentDidMount() { Linking.getInitialURL().then((url) => { if (url) { console.log('Initial url is: ' + url); } }).catch(err => console.error('An error ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found