@@ -22,83 +22,91 @@ interface BeforeInstallPromptEvent extends Event {
2222}
2323
2424const page = ( ) => {
25- const [ deferredPrompt , setDeferredPrompt ] = useState ( null ) ;
26- const [ isIOS , setIsIOS ] = useState ( false ) ;
27- const [ isAndroid , setIsAndroid ] = useState ( false ) ;
28-
25+ const [ deferredPrompt , setDeferredPrompt ] =
26+ useState < BeforeInstallPromptEvent | null > ( null ) ;
27+ const [ platform , setPlatform ] = useState < 'ios' | 'android' | 'other' > (
28+ 'other'
29+ ) ;
30+ const [ isKakaoInApp , setIsKakaoInApp ] = useState ( false ) ;
2931 const [ showModal , setShowModal ] = useState ( false ) ;
3032
3133 useEffect ( ( ) => {
3234 const userAgent = window . navigator . userAgent . toLowerCase ( ) ;
33- const isIOSDevice = / i p h o n e | i p a d | i p o d / . test ( userAgent ) ;
34- const isAndroidDevice = / a n d r o i d / . test ( userAgent ) ;
35+ const isIOS = / i p h o n e | i p a d | i p o d / . test ( userAgent ) ;
36+ const isAndroid = / a n d r o i d / . test ( userAgent ) ;
37+ const isKakao = / k a k a o t a l k / i. test ( userAgent ) ;
3538
36- setIsIOS ( isIOSDevice ) ;
37- setIsAndroid ( isAndroidDevice ) ;
39+ setPlatform ( isIOS ? 'ios' : isAndroid ? 'android' : 'other' ) ;
40+ setIsKakaoInApp ( isKakao ) ;
3841
39- // Android์์๋ง beforeinstallprompt ์ด๋ฒคํธ ์ฒ๋ฆฌ
40- if ( isAndroidDevice ) {
41- const handler = ( e : any ) => {
42- console . log ( '๐ฅ beforeinstallprompt ๋ฐ์' ) ;
43- e . preventDefault ( ) ;
44- setDeferredPrompt ( e ) ;
45- } ;
42+ if ( isKakao ) {
43+ if ( isAndroid ) {
44+ const currentUrl = window . location . href ;
45+ const intentUrl = `intent://${ currentUrl . replace (
46+ / ^ h t t p s ? : \/ \/ / ,
47+ ''
48+ ) } #Intent;scheme=https;package=com.android.chrome;end`;
49+ window . location . href = intentUrl ;
50+ } else if ( isIOS ) {
51+ alert (
52+ 'ํ๋ฉด ์ฐ์ธก ํ๋จ ๊ณต์ ํ๊ธฐ ๋ฉ๋ด์์ "Safari๋ก ์ด๊ธฐ"๋ฅผ ์ ํํด์ฃผ์ธ์.'
53+ ) ;
54+ }
55+ } else {
56+ if ( isAndroid ) {
57+ const handler = ( e : any ) => {
58+ e . preventDefault ( ) ;
59+ setDeferredPrompt ( e ) ;
60+ } ;
61+ window . addEventListener ( 'beforeinstallprompt' , handler ) ;
62+ return ( ) => window . removeEventListener ( 'beforeinstallprompt' , handler ) ;
63+ }
4664
47- window . addEventListener ( 'beforeinstallprompt' , handler ) ;
48- return ( ) => {
49- window . removeEventListener ( 'beforeinstallprompt' , handler ) ;
50- } ;
65+ // ์ธ๋ถ ๋ธ๋ผ์ฐ์ ์ผ ๊ฒฝ์ฐ ์ง์
์ ์ค์น ๋ชจ๋ฌ ์๋ ์คํ
66+ setShowModal ( true ) ;
5167 }
5268 } , [ ] ) ;
5369
54- const handleInstallClick = async ( ) => {
55- if ( isAndroid && deferredPrompt ) {
70+ const handleInstall = async ( ) => {
71+ if ( platform === 'android' && deferredPrompt ) {
5672 deferredPrompt . prompt ( ) ;
5773 const { outcome } = await deferredPrompt . userChoice ;
58- console . log ( 'Android ์ฌ์ฉ์ ์ ํ:' , outcome ) ;
74+ console . log ( '์ค์น ๊ฒฐ๊ณผ:' , outcome ) ;
75+ if ( outcome === 'accepted' ) {
76+ alert ( '์ค์น๊ฐ ์๋ฃ๋์์ต๋๋ค!' ) ;
77+ } else {
78+ alert ( '์ค์น๊ฐ ์ทจ์๋์์ต๋๋ค.' ) ;
79+ }
5980 setDeferredPrompt ( null ) ;
6081 setShowModal ( false ) ;
61- } else if ( isIOS ) {
62- // iOS๋ ์๋ด๋ง
63- alert (
64- 'iPhone ์ฌ์ฉ์๋ Safari์์ ๊ณต์ ๋ฒํผ์ ๋๋ฌ [ํ ํ๋ฉด์ ์ถ๊ฐ]๋ฅผ ์ ํํด์ฃผ์ธ์!'
65- ) ;
82+ } else if ( platform === 'ios' ) {
83+ alert ( 'ํ๋จ ๊ณต์ ๋ฒํผ์ ๋๋ฅด๊ณ "ํ ํ๋ฉด์ ์ถ๊ฐ"๋ฅผ ์ ํํด์ฃผ์ธ์.' ) ;
6684 setShowModal ( false ) ;
6785 } else {
68- alert ( '์ค์น ๊ธฐ๋ฅ์ด ํ์ฌ ํ๊ฒฝ์์ ์ง์๋์ง ์์ต๋๋ค.' ) ;
86+ alert ( 'ํ์ฌ ํ๊ฒฝ์์๋ ์ค์น๊ฐ ์ง์๋์ง ์์ต๋๋ค.' ) ;
6987 setShowModal ( false ) ;
7088 }
7189 } ;
7290
73- const handleShowModal = ( ) => {
74- if ( isAndroid && deferredPrompt ) {
75- setShowModal ( true ) ;
76- } else if ( isIOS ) {
77- setShowModal ( true ) ;
78- } else {
79- alert ( '๋ธ๋ผ์ฐ์ ๊ฐ ์์ง ์ค์น๋ฅผ ํ์ฉํ์ง ์์์ต๋๋ค.' ) ;
80- }
81- } ;
82-
8391 return (
8492 < Container >
8593 < Title > PWA ์ค์น ์ ๋ TEST</ Title >
8694 < Button
8795 buttonType = "primary"
8896 text = "์ฑ ์ค์นํ๊ธฐ"
89- onClick = { handleShowModal }
97+ onClick = { ( ) => setShowModal ( true ) }
9098 />
9199 { showModal && (
92100 < ConfirmModal
93- title = "PWA ์ค์นํ๊ธฐ"
101+ title = "์ฑ ์ค์นํ๊ธฐ"
94102 sub = {
95- isIOS
96- ? 'Safari ๊ณต์ ๋ฒํผ์ ๋๋ฌ [ ํ ํ๋ฉด์ ์ถ๊ฐ]๋ฅผ ์ ํํด์ฃผ์ธ์. '
97- : 'ํ ํ๋ฉด์ ์ถ๊ฐํ์๊ฒ ์ด์ ?'
103+ platform === 'ios'
104+ ? 'ํ ํ๋ฉด์ ์ถ๊ฐํ์๊ฒ ์ด์? '
105+ : '์ฑ์ ์ค์นํ์๊ฒ ์ด์ ?'
98106 }
99- confirmText = { isIOS ? '์๊ฒ ์ต๋๋ค' : ' ์ค์นํ๊ธฐ' }
107+ confirmText = " ์ค์นํ๊ธฐ"
100108 cancelText = "์ทจ์"
101- onConfirm = { handleInstallClick }
109+ onConfirm = { handleInstall }
102110 onCancel = { ( ) => setShowModal ( false ) }
103111 />
104112 ) }
@@ -114,7 +122,8 @@ const Container = styled.div`
114122 padding: 100px 30px;
115123 height: 100vh;
116124 flex-direction: column;
117- justify-content: space-between;
125+ justify-content: center;
126+ gap: 20px;
118127 background-image: url('/assets/login/login_bg.png');
119128 background-size: cover;
120129 background-position: center;
@@ -126,5 +135,6 @@ const Container = styled.div`
126135
127136const Title = styled . h1 `
128137 color: ${ theme . colors . white } ;
138+ ${ theme . fonts . title01 }
129139 text-align: center;
130140` ;
0 commit comments