Linking.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const InteractionManager = require('../Interaction/InteractionManager');
  12. const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
  13. const Platform = require('../Utilities/Platform');
  14. const invariant = require('invariant');
  15. import NativeLinking from './NativeLinking';
  16. /**
  17. * `Linking` gives you a general interface to interact with both incoming
  18. * and outgoing app links.
  19. *
  20. * See https://reactnative.dev/docs/linking.html
  21. */
  22. class Linking extends NativeEventEmitter {
  23. constructor() {
  24. super(NativeLinking);
  25. }
  26. /**
  27. * Add a handler to Linking changes by listening to the `url` event type
  28. * and providing the handler
  29. *
  30. * See https://reactnative.dev/docs/linking.html#addeventlistener
  31. */
  32. addEventListener(type: string, handler: Function) {
  33. this.addListener(type, handler);
  34. }
  35. /**
  36. * Remove a handler by passing the `url` event type and the handler.
  37. *
  38. * See https://reactnative.dev/docs/linking.html#removeeventlistener
  39. */
  40. removeEventListener(type: string, handler: Function) {
  41. this.removeListener(type, handler);
  42. }
  43. /**
  44. * Try to open the given `url` with any of the installed apps.
  45. *
  46. * See https://reactnative.dev/docs/linking.html#openurl
  47. */
  48. openURL(url: string): Promise<any> {
  49. this._validateURL(url);
  50. return NativeLinking.openURL(url);
  51. }
  52. /**
  53. * Determine whether or not an installed app can handle a given URL.
  54. *
  55. * See https://reactnative.dev/docs/linking.html#canopenurl
  56. */
  57. canOpenURL(url: string): Promise<boolean> {
  58. this._validateURL(url);
  59. return NativeLinking.canOpenURL(url);
  60. }
  61. /**
  62. * Open app settings.
  63. *
  64. * See https://reactnative.dev/docs/linking.html#opensettings
  65. */
  66. openSettings(): Promise<any> {
  67. return NativeLinking.openSettings();
  68. }
  69. /**
  70. * If the app launch was triggered by an app link,
  71. * it will give the link url, otherwise it will give `null`
  72. *
  73. * See https://reactnative.dev/docs/linking.html#getinitialurl
  74. */
  75. getInitialURL(): Promise<?string> {
  76. return Platform.OS === 'android'
  77. ? InteractionManager.runAfterInteractions().then(() =>
  78. NativeLinking.getInitialURL(),
  79. )
  80. : NativeLinking.getInitialURL();
  81. }
  82. /*
  83. * Launch an Android intent with extras (optional)
  84. *
  85. * @platform android
  86. *
  87. * See https://reactnative.dev/docs/linking.html#sendintent
  88. */
  89. sendIntent(
  90. action: string,
  91. extras?: Array<{
  92. key: string,
  93. value: string | number | boolean,
  94. ...
  95. }>,
  96. ): Promise<void> {
  97. if (Platform.OS === 'android') {
  98. return NativeLinking.sendIntent(action, extras);
  99. }
  100. return new Promise((resolve, reject) => reject(new Error('Unsupported')));
  101. }
  102. _validateURL(url: string) {
  103. invariant(
  104. typeof url === 'string',
  105. 'Invalid URL: should be a string. Was: ' + url,
  106. );
  107. invariant(url, 'Invalid URL: cannot be empty');
  108. }
  109. }
  110. module.exports = (new Linking(): Linking);