-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
index.js
143 lines (133 loc) · 4.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* InAppBrowser for React Native
* https://github.com/proyecto26/react-native-inappbrowser
*
* @format
* @flow strict-local
*/
import type {
BrowserResult,
AuthSessionResult,
InAppBrowserOptions,
} from './types';
import {
isAndroid,
RNInAppBrowser,
openBrowserAsync,
openAuthSessionAsync,
openAuthSessionPolyfillAsync,
closeAuthSessionPolyfillAsync,
authSessionIsNativelySupported,
} from './utils';
/**
* Opens the url with Safari in a modal on iOS using [`SFSafariViewController`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller),
* or Chrome in a new [custom tab](https://developer.chrome.com/multidevice/android/customtabs) on Android.
*
* @param url The url to open in the web browser.
* @param options A dictionary of key-value pairs.
*
* @return The promise behaves differently based on the platform:
* - If the user closed the web browser, the Promise resolves with `{ type: 'cancel' }`.
* - If the browser is closed using [`close`](#webbrowserdismissbrowser), the Promise resolves with `{ type: 'dismiss' }`.
*/
async function open(
url: string,
options?: InAppBrowserOptions
): Promise<BrowserResult> {
return openBrowserAsync(url, options);
}
/**
* # On iOS:
* Opens the url with Safari in a modal using `ASWebAuthenticationSession`. The user will be asked
* whether to allow the app to authenticate using the given url.
*
* # On Android:
* This will be done using a "custom Chrome tabs" browser and [activityResumedEvent](https://docs.nativescript.org/api-reference/classes/androidapplication#activityresumedevent),
*
* @param url The url to open in the web browser. This should be a login page.
* @param redirectUrl _Optional_ - The url to deep link back into your app.
* @param options _Optional_ - An object extending the InAppBrowser Options.
*
* @return
* - If the user does not permit the application to authenticate with the given url, the Promise fulfills with `{ type: 'cancel' }` object.
* - If the user closed the web browser, the Promise fulfills with `{ type: 'cancel' }` object.
* - If the browser is closed using `dismissBrowser`, the Promise fulfills with `{ type: 'dismiss' }` object.
*/
async function openAuth(
url: string,
redirectUrl: string,
options?: InAppBrowserOptions
): Promise<AuthSessionResult> {
if (authSessionIsNativelySupported()) {
return openAuthSessionAsync(url, redirectUrl, options);
} else {
return openAuthSessionPolyfillAsync(url, redirectUrl, options);
}
}
/**
* Dismisses the presented web browser.
*/
function close(): void {
RNInAppBrowser.close();
}
/**
* Warm up the browser process.
* Allows the browser application to pre-initialize itself in the background.
* Significantly speeds up URL opening in the browser.
* This is asynchronous and can be called several times.
*
* @platform android
*/
function warmup(): Promise<boolean> {
if (isAndroid) {
return RNInAppBrowser.warmup();
}
return Promise.resolve(false);
}
/**
* Tells the browser of a likely future navigation to a URL.
* The most likely URL has to be specified first.
* Optionally, a list of other likely URLs can be provided.
* They are treated as less likely than the first one, and have to be sorted in decreasing priority order.
* These additional URLs may be ignored.
*
* @param mostLikelyUrl Most likely URL, may be null if otherUrls is provided.
* @param otherUrls Other likely destinations, sorted in decreasing likelihood order.
*
* @platform android
*/
function mayLaunchUrl(
mostLikelyUrl: string,
otherUrls: Array<string> = []
): void {
if (isAndroid) {
RNInAppBrowser.mayLaunchUrl(mostLikelyUrl, otherUrls);
}
}
/**
* Dismisses the current authentication session
*/
function closeAuth(): void {
closeAuthSessionPolyfillAsync();
if (authSessionIsNativelySupported()) {
RNInAppBrowser.closeAuth();
} else {
close();
}
}
/**
* Detect if the device supports this plugin.
*/
async function isAvailable(): Promise<boolean> {
return RNInAppBrowser.isAvailable();
}
export const InAppBrowser = {
open,
openAuth,
close,
closeAuth,
isAvailable,
warmup,
mayLaunchUrl,
};
export default InAppBrowser;