react-native-macos/Libraries/Linking/Linking.js

125 строки
3.1 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const InteractionManager = require('../Interaction/InteractionManager');
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
const Platform = require('../Utilities/Platform');
const invariant = require('invariant');
import NativeLinking from './NativeLinking';
/**
* `Linking` gives you a general interface to interact with both incoming
* and outgoing app links.
*
* See https://reactnative.dev/docs/linking.html
*/
class Linking extends NativeEventEmitter {
constructor() {
super(NativeLinking);
}
/**
* Add a handler to Linking changes by listening to the `url` event type
* and providing the handler
*
* See https://reactnative.dev/docs/linking.html#addeventlistener
*/
addEventListener(type: string, handler: Function) {
this.addListener(type, handler);
}
/**
* Remove a handler by passing the `url` event type and the handler.
*
* See https://reactnative.dev/docs/linking.html#removeeventlistener
*/
removeEventListener(type: string, handler: Function) {
this.removeListener(type, handler);
}
/**
* Try to open the given `url` with any of the installed apps.
*
* See https://reactnative.dev/docs/linking.html#openurl
*/
openURL(url: string): Promise<any> {
this._validateURL(url);
return NativeLinking.openURL(url);
}
/**
* Determine whether or not an installed app can handle a given URL.
*
* See https://reactnative.dev/docs/linking.html#canopenurl
*/
canOpenURL(url: string): Promise<boolean> {
this._validateURL(url);
return NativeLinking.canOpenURL(url);
}
- Add openSettings method to Linking module (#23965) Summary: This will create a cross-platform and safe way to programmatically open the app's settings into the iOS /Android Settings app. Right now it's possible to open the app's settings, but _**only for iOS**_ via `Linking.openURL("app-settings:")` To do the same for Android, you need to either create NodeModule or install a dependency such as [react-native-open-settings](https://github.com/lunarmayor/react-native-open-settings). Why this new method is useful: since Android 6, app permissions work similar to iOS. It's granular and it's requested in the app runtime. https://developer.android.com/guide/topics/permissions/overview#runtime_requests_android_60_and_higher > If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime. When your app requests permission, the user sees a system dialog telling the user which permission group your app is trying to access. The dialog includes a Deny and Allow button. Thus, if the user checks the **"Never ask again box"** and taps **"Deny"**, for some specific permission, the only way to change the permission is going to the Android Setting app. And that's where this new method becomes useful. It'll allow our apps to programmatically send the the user to settings app. Also, `openSettings()` doesn't receive a parameter to redirect to specific subsections of the Settings app because there's no public API to do it on iOS ([there's a way to have, via private API, but it causes the app to get rejected.](https://github.com/mauron85/cordova-plugin-background-geolocation/issues/394)) Create `Linking.openSettings()` for iOS and Android; [General] [add ] - Add openSetting method to Linking module Pull Request resolved: https://github.com/facebook/react-native/pull/23965 Differential Revision: D14502910 Pulled By: cpojer fbshipit-source-id: d27d62282b9df499845c78d983d3b6936c36ea39
2019-03-18 18:03:05 +03:00
/**
* Open app settings.
*
* See https://reactnative.dev/docs/linking.html#opensettings
- Add openSettings method to Linking module (#23965) Summary: This will create a cross-platform and safe way to programmatically open the app's settings into the iOS /Android Settings app. Right now it's possible to open the app's settings, but _**only for iOS**_ via `Linking.openURL("app-settings:")` To do the same for Android, you need to either create NodeModule or install a dependency such as [react-native-open-settings](https://github.com/lunarmayor/react-native-open-settings). Why this new method is useful: since Android 6, app permissions work similar to iOS. It's granular and it's requested in the app runtime. https://developer.android.com/guide/topics/permissions/overview#runtime_requests_android_60_and_higher > If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime. When your app requests permission, the user sees a system dialog telling the user which permission group your app is trying to access. The dialog includes a Deny and Allow button. Thus, if the user checks the **"Never ask again box"** and taps **"Deny"**, for some specific permission, the only way to change the permission is going to the Android Setting app. And that's where this new method becomes useful. It'll allow our apps to programmatically send the the user to settings app. Also, `openSettings()` doesn't receive a parameter to redirect to specific subsections of the Settings app because there's no public API to do it on iOS ([there's a way to have, via private API, but it causes the app to get rejected.](https://github.com/mauron85/cordova-plugin-background-geolocation/issues/394)) Create `Linking.openSettings()` for iOS and Android; [General] [add ] - Add openSetting method to Linking module Pull Request resolved: https://github.com/facebook/react-native/pull/23965 Differential Revision: D14502910 Pulled By: cpojer fbshipit-source-id: d27d62282b9df499845c78d983d3b6936c36ea39
2019-03-18 18:03:05 +03:00
*/
openSettings(): Promise<any> {
return NativeLinking.openSettings();
- Add openSettings method to Linking module (#23965) Summary: This will create a cross-platform and safe way to programmatically open the app's settings into the iOS /Android Settings app. Right now it's possible to open the app's settings, but _**only for iOS**_ via `Linking.openURL("app-settings:")` To do the same for Android, you need to either create NodeModule or install a dependency such as [react-native-open-settings](https://github.com/lunarmayor/react-native-open-settings). Why this new method is useful: since Android 6, app permissions work similar to iOS. It's granular and it's requested in the app runtime. https://developer.android.com/guide/topics/permissions/overview#runtime_requests_android_60_and_higher > If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime. When your app requests permission, the user sees a system dialog telling the user which permission group your app is trying to access. The dialog includes a Deny and Allow button. Thus, if the user checks the **"Never ask again box"** and taps **"Deny"**, for some specific permission, the only way to change the permission is going to the Android Setting app. And that's where this new method becomes useful. It'll allow our apps to programmatically send the the user to settings app. Also, `openSettings()` doesn't receive a parameter to redirect to specific subsections of the Settings app because there's no public API to do it on iOS ([there's a way to have, via private API, but it causes the app to get rejected.](https://github.com/mauron85/cordova-plugin-background-geolocation/issues/394)) Create `Linking.openSettings()` for iOS and Android; [General] [add ] - Add openSetting method to Linking module Pull Request resolved: https://github.com/facebook/react-native/pull/23965 Differential Revision: D14502910 Pulled By: cpojer fbshipit-source-id: d27d62282b9df499845c78d983d3b6936c36ea39
2019-03-18 18:03:05 +03:00
}
/**
* If the app launch was triggered by an app link,
* it will give the link url, otherwise it will give `null`
*
* See https://reactnative.dev/docs/linking.html#getinitialurl
*/
getInitialURL(): Promise<?string> {
return Platform.OS === 'android'
? InteractionManager.runAfterInteractions().then(() =>
NativeLinking.getInitialURL(),
)
: NativeLinking.getInitialURL();
}
/*
* Launch an Android intent with extras (optional)
*
* @platform android
*
* See https://reactnative.dev/docs/linking.html#sendintent
*/
sendIntent(
action: string,
extras?: Array<{
key: string,
value: string | number | boolean,
...
}>,
): Promise<void> {
if (Platform.OS === 'android') {
return NativeLinking.sendIntent(action, extras);
}
return new Promise((resolve, reject) => reject(new Error('Unsupported')));
}
_validateURL(url: string) {
invariant(
typeof url === 'string',
'Invalid URL: should be a string. Was: ' + url,
);
invariant(url, 'Invalid URL: cannot be empty');
}
}
module.exports = (new Linking(): Linking);