Skip to content

Commit 9a8778c

Browse files
authored
Merge pull request #181 from ASAP-Lettering/test/#180
[Test] 카카오 인앱 구분 테스트
2 parents e57022d + 57d1294 commit 9a8778c

1 file changed

Lines changed: 56 additions & 46 deletions

File tree

src/app/test/page.tsx

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,83 +22,91 @@ interface BeforeInstallPromptEvent extends Event {
2222
}
2323

2424
const 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 = /iphone|ipad|ipod/.test(userAgent);
34-
const isAndroidDevice = /android/.test(userAgent);
35+
const isIOS = /iphone|ipad|ipod/.test(userAgent);
36+
const isAndroid = /android/.test(userAgent);
37+
const isKakao = /kakaotalk/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+
/^https?:\/\//,
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

127136
const Title = styled.h1`
128137
color: ${theme.colors.white};
138+
${theme.fonts.title01}
129139
text-align: center;
130140
`;

0 commit comments

Comments
 (0)