GithubHelp home page GithubHelp logo

starotitorov / react-native-deep-link Goto Github PK

View Code? Open in Web Editor NEW
144.0 4.0 13.0 1.31 MB

NPM package for handling deep links in React Native applications (Android+iOS).

License: MIT License

JavaScript 64.21% Python 8.14% Java 8.12% Objective-C 19.53%
react-native react javascript android ios deep-linking

react-native-deep-link's Introduction

react-native-deep-link

Build Status npm version NPM Downloads

NPM

React Native deep linking library. If you need to handle deep links in your project, just create a schemes config, the package will do the rest!

Why do I need this package?

This package allows you to handle deep links in React Native applications. The package can be added to your project irrespective of what solution you are using for navigation, what state management library you decided to use. I will try to provide some examples below just to show why you may need this package.

If you are using react-navigation, you should know that it supports deep linking out of the box, but sometimes this solution does not meet your needs well.

React-navigation deep linking implementation only allows you to navigate user to some screen when application receives the url. This package provides you an ability to decide how to handle the url by specifying your own handler in the config, read the docs below.

Also, in real applications it is a common practice to add navigation state to Redux. According to the react-navigation documentation, in this case you have to handle deep links manually, react-native-deep-link provides a solution. Adding navigation to Redux gives you more control on the navigation state, allows to dispatch navigation actions from your redux-thunk actions.

The package does not require Redux as a dependency, so you can use it in your React Native apps without Redux. For example, you can implement your own NavigationService as it is described in react-navigation docs and use it in route callbacks, read the docs below.

Installation

yarn add react-native-deep-link
# or with npm
# npm install --save react-native-deep-link

Configuring Android

For instructions on how to add support for deep linking on Android, refer to Enabling Deep Links for App Content - Add Intent Filters for Your Deep Links.

If you wish to receive the intent in an existing instance of MainActivity, you may set the launchMode of MainActivity to singleTask in AndroidManifest.xml. See <activity> documentation for more information.

<activity
  android:name=".MainActivity"
  android:launchMode="singleTask">

Configuring iOS

On iOS, you'll need to link RCTLinking to your project by following the steps described here. If you also want to listen to incoming app links during your app's execution, you'll need to add the following lines to your *AppDelegate.m:

// iOS 9.x or newer
#import <React/RCTLinkingManager.h>

- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

If you're targeting iOS 8.x or older, you can use the following code instead:

// iOS 8.x or older
#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];
}

If your app is using Universal Links, you'll need to add the following code as well:

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
 restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
 return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                  restorationHandler:restorationHandler];
}

Examples

An Example of usage this package in the Redux application available in the example/ folder.

In the NavigationServiceExample/ folder you can find an example of usage the package in the application without Redux.

Usage

After installing the package, you need to follow a few simple steps:

  1. Use createDeepLinkingHandler to get a higher order component, pass schemes config to this function.
import { createDeepLinkingHandler } from 'react-native-deep-link';

/**
 * This function receives a result of url parsing,
 * you can find the structure of this object in the API docs below, and returns a function.
 * The returned function receives component props.
 */
const handleInvitationToChannel = ({ params: { channelId } }) => ({ dispatch }) => {
    // addCurrentUserToChannel is a redux-thunk action,
    // which was defined somewhere in the code.
    dispatch(addCurrentUserToChannel(channelId));
}

const schemes = [
    {
        name: 'example:',
        routes: [
            {
                expression: '/channels/:channelId',
                callback: handleInvitationToChannel
            }
        ]
    }
];

const withDeepLinking = createDeepLinkingHandler(schemes);
  1. Use the higher-order component returned from createDeepLinkingHandler.
export default compose(
    connect(mapStateToProps, mapDispatchToProps),
    withDeepLinking
)(App);

That was it, you have added react-native-deep-link package to the project!

Optionally, if you need to handle situations, when the url is not supported or a handler was not found for the url, you can pass callbacks to withDeepLinking:

import { createDeepLinkingHandler } from 'react-native-deep-link';

const App = createDeepLinkingHandler(schemes)(AppComponent);

class Root extends Component {
    render() {
        return (
            <App
                onGetInitialUrlError={err => console.log(err)}
                onCanOpenUrlError={err => console.log(err)}
                onUrlIsNotSupported={url => console.log(`The ${url} is not supported.`)}
                onCannotHandleUrl={url => console.log(`A handler for the ${url} was not found.`)}
            />
        );
    }
}

API

createDeepLinkingHandler takes an array of schemes as a parameter. Each scheme should have a name and an array of routes.

Each route should have an expression and a callback to be invoked in case of successful matching of the url to the route specified using an expression property. Follow the next pattern to specify named url parameters :<parameter_name>. Examples: /users/:userId, /conversations/:conversationId/messages/:messageId.

Route callback is a higher-order function which receives the result of url parsing and returns a function. This returned function receives component props.

A result of url parsing is an object with the next set of properties:

{
    scheme: 'example:', // Scheme name
    route: '/colors/:color', // Route expression
    query: {}, // Query string parameters
    params: {} // Url parameters
}

createDeepLinkingHandler returns a higher order component, the next props can be passed to this component:

Property Type Optional Default Description
onGetInitialUrlError function true () => {} a callback, which will be called in case the application throws an error trying to get initial url. The function receives the error.
onCanOpenUrlError function true () => {} a callback, which will be called in case the application throws an error trying to open url. The function receives the error.
onUrlIsNotSupported function true () => {} a callback, which will be called in case the application does not support received url. The function receives the url.
onCannotHandleUrl function true () => {} a callback, which will be called in case a handler for the given url was not specified. The function receives the url.

Contributing

You are welcome! Create pull requests and help to improve the package.

License

react-native-deep-link is licensed under the MIT License.

react-native-deep-link's People

Contributors

starotitorov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-native-deep-link's Issues

Does it work for react-native android perfectly ?

I have been searching for react-native deep linking in react-native project. Finally I got it this project, Can I get this package correctly. I mean does it work for IOS and Android ?
Please let me know thanks

Without redux

Hi can I use this library without use redux?
Thanks

A handler for the was not found.

Hi thank you for your library,
I get following error:
A handler for the https://www.exmple.com/football/slug/ was not found.

but i have this schema


const schemes = [
	{
		name: 'https://www.exmple.com',
		routes: [
			{
				expression: '/football/:slug/',
				callback: handleMatchPreview
			},
			{
				expression: '/football/:slug',
				callback: handleMatchPreview
			},
			{
				expression: ':slug',
				callback: handleMatchPreview
			},
			{
				expression: '/',
				callback: handleMatchPreview
			},
		]
	}, {
		name: 'https://www.exmple.com/',
		routes: [
			{
				expression: '/football/:slug/',
				callback: handleMatchPreview
			},
			{
				expression: '/football/:slug',
				callback: handleMatchPreview
			},
			{
				expression: ':slug',
				callback: handleMatchPreview
			},
			{
				expression: '/',
				callback: handleMatchPreview
			},
		]
	},
];

How do you deal with dynamic queries?

I have this setup:

export default createDeepLinkingHandler([{
name: 'exampledomain',
routes: [{
expression: '/chat/:merchant',
callback: handleMerchantMessage
},
{
expression: '/callbackuri',
callback: handleCallBack
}
]
}]);

However I am getting values like so:
exampledomain://callbackUri?state=faddf1f1-a667-4248-bfc0-0b0d42c3a315&session_state=b15b2e24-28fb-4b4c-959f-6ae3525f209d&code=b1e1e052-c46b-416f-ab75-6aafc2403883.b15b2e24-28fb-4b4c-959f-6ae3525f209d.c563ae1c-939a-4c97-82f2-6e92a5b6f8c3 was not found.

How am I supposed to deal with this?

Error: The route /chat/:merchant callback is not a higher order function.

I have the following scheme:

const schemes = [
    {
        name: 'company:',
        routes: [
            {
                expression: '/chat/:merchant',
                callback: handleMerchantMessage
            },
            {
                expression: '/detail/:merchant',
                callback: handleMerchantNotice
            },
            {
                expression: '/callbackuri',
                callback: handleCallBack,
                query: {
                    "state": "state",
                    "session_state": "session_state",
                    "code": "code",
                }
            }
        ]
    },
    {
        name: 'https:',
        routes: [
            {
                expression: 'https://company.onelink.me/QOVp/d8e6a2e0?af_dp=:merchant',
                callback: handleDeferredDeeplink
            }
        ]
    }
]

export default createDeepLinkingHandler(schemes);

And the following functions:

export const handleMerchantMessage = () => {
    pushNotificationBase();
}

export const handleMerchantNotice = () => {
    pushNotificationBase();
}

export const handleDeferredDeeplink = () => {
}

export const handleCallBack = () => {
    pushNotificationBase();
}

I get:

Error: The route /chat/:merchant callback is not a higher order function.

These look very similar to your example in handlers... why am I receiving the above error message? How do I change my code to fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.