-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathutils.ts
More file actions
78 lines (74 loc) · 2.34 KB
/
Copy pathutils.ts
File metadata and controls
78 lines (74 loc) · 2.34 KB
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
import {
Alert,
Platform,
StatusBar,
Linking,
StatusBarStyle,
} from 'react-native';
import {InAppBrowser} from 'react-native-inappbrowser-reborn';
const sleep = (timeout: number) =>
new Promise<void>(resolve => setTimeout(resolve, timeout));
export const openLink = async (
url: string,
statusBarStyle: StatusBarStyle,
animated = true,
) => {
try {
// const {width, height} = Dimensions.get('window');
if (await InAppBrowser.isAvailable()) {
// A delay to change the StatusBar when the browser is opened
const delay = animated && Platform.OS === 'ios' ? 400 : 0;
setTimeout(() => StatusBar.setBarStyle('light-content'), delay);
const options = {
// iOS Properties
ephemeralWebSession: true,
// Android Properties
incognito: true,
};
console.log('openLink -> options', options);
const result = await InAppBrowser.openAuth(url, url, options);
// A delay to show an alert when the browser is closed
await sleep(800);
Alert.alert('Response', JSON.stringify(result));
} else {
Linking.openURL(url);
}
} catch (error) {
await sleep(50);
const errorMessage = (error as Error).message || (error as string);
Alert.alert(errorMessage);
} finally {
// Restore the previous StatusBar of the App
StatusBar.setBarStyle(statusBarStyle);
}
};
export const getDeepLink = (path = '') => {
const scheme = 'my-demo';
const prefix =
Platform.OS === 'android' ? `${scheme}://demo/` : `${scheme}://`;
return prefix + path;
};
export const tryDeepLinking = async () => {
const loginUrl = 'https://proyecto26.github.io/react-native-inappbrowser/';
const redirectUrl = getDeepLink();
const url = `${loginUrl}?redirect_url=${encodeURIComponent(redirectUrl)}`;
try {
if (await InAppBrowser.isAvailable()) {
const result = await InAppBrowser.openAuth(url, redirectUrl, {
// iOS Properties
ephemeralWebSession: false,
// Android Properties
showTitle: false,
enableUrlBarHiding: true,
enableDefaultShare: false,
});
await sleep(800);
Alert.alert('Response', JSON.stringify(result));
} else {
Alert.alert('InAppBrowser is not supported :/');
}
} catch (error) {
console.error(error);
Alert.alert('Something’s wrong with the app :(');
}
};