-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
99 lines (94 loc) · 2.97 KB
/
App.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
import React, { useEffect, useState, useRef, useCallback } from "react";
import {
StatusBar,
View,
StyleSheet,
BackHandler,
Alert,
Platform,
} from "react-native";
import { WebView } from "react-native-webview";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
export default function App() {
const webview = useRef();
const [isCanGoBack, setIsCanGoBack] = useState(false);
const onPressHardwareBackButton = () => {
if (isCanGoBack) {
webview.current.goBack();
return true;
} else {
// Alert.alert("", "앱을 종료하시겠습니까?", [
// {
// text: "취소",
// onPress: () => null,
// },
// { text: "확인", onPress: () => BackHandler.exitApp() },
// ]);
// return true;
// }
return false;
}
};
useEffect(() => {
BackHandler.addEventListener(
"hardwareBackPress",
onPressHardwareBackButton
);
return () => {
BackHandler.removeEventListener(
"hardwareBackPress",
onPressHardwareBackButton
);
};
}, [isCanGoBack]);
return Platform.OS === "ios" ? (
<SafeAreaView style={{ flex: 1 }} edges={["top"]} backgroundColor="#FBFBFB">
<WebView
userAgent={`Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Mobile/15E148 Safari/604.1`}
source={{ uri: "https://myplanit.site" }}
/>
<StatusBar barStyle="dark-content" backgroundColor="#FBFBFB" />
</SafeAreaView>
) : (
<View style={{ flex: 1, marginTop: 30 }}>
<WebView
userAgent={`Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Mobile Safari/537.36`}
source={{ uri: "https://myplanit.site" }}
ref={webview}
injectedJavaScript={`
(function() {
function wrap(fn) {
return function wrapper() {
var res = fn.apply(this, arguments);
window.ReactNativeWebView.postMessage(window.location.href);
return res;
}
}
history.pushState = wrap(history.pushState);
history.replaceState = wrap(history.replaceState);
window.addEventListener('popstate', function() {
window.ReactNativeWebView.postMessage(window.location.href);
});
})();
true;
`}
onMessage={({ nativeEvent: state }) => {
console.log(state.data);
if (state.data !== "https://www.myplanit.site/todo") {
// Navigation state updated, can check state.canGoBack, etc.
setIsCanGoBack(state.canGoBack);
console.log(isCanGoBack);
} else {
setIsCanGoBack(false);
console.log(isCanGoBack);
}
}}
/>
<StatusBar
translucent
barStyle="dark-content"
backgroundColor="transparent"
/>
</View>
);
}