devedv
devedv

Reputation: 632

Implementing deep link in react native

I’m Implementing deep link in react native app to click on View in app A to open app B. Before calling URL directly from another app I wanted to test it if it is working or not,

So for android it worked. I just set the launch option to URL and it showed the value of URL in console.

But in iPhone if I write the URL(testlink://) in safari browser it shows “safari cannot open the page because address is invalid”.

Steps I followed to setup deeplinking:

  1. Added following code in AppDelgate.m

    #import <React/RCTLinkingManager.h>
    
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
      return [RCTLinkingManager application:application openURL:url
                          sourceApplication:sourceApplication annotation:annotation];
    }
    
    - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
     restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
    {
     return [RCTLinkingManager application:application
                      continueUserActivity:userActivity
                        restorationHandler:restorationHandler];
    }

  1. Added a key in info.plist in XCode.

    URLTypes -> Item 0 -> URL identifier(VALUE: testlink)

  2. Adding the following code in Home page of the App.

   

 componentDidMount() {
      Linking.addEventListener('url', this._handleOpenURL);
    },
    componentWillUnmount() {
      Linking.removeEventListener('url', this._handleOpenURL);
    },
    _handleOpenURL(event) {
      console.log(event.url);
    }

Upvotes: 1

Views: 2801

Answers (1)

Dan
Dan

Reputation: 8844

Have you added testlink:// as a URL type in XCode?

For the purpose of this example, my Bundle Identifier is: org.reactjs.native.example.MyApp

  1. Click on your project in Xcode, and click on the Info tab
  2. Scroll down to URL Types
  3. Click the + button
  4. Add the Identifier: org.reactjs.native.example.MyApp.linking
  5. Add URL Schemes: testlink
  6. Clean & Build
  7. Open Safari and type in testlink:// - it should open your app.

Upvotes: 7

Related Questions