react native에서 위치 액세스 권한을 허용하지 않았을 때,
앱 설정으로 보내 액세스 권한 설정 후, 다시 돌아오면 권한을 확인하여 메인페이지로 이동시키려고 코드를 아래와 같이 작성했다.
useEffect(() => {
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("hi");
// requestPermissionFunc();
}
appState.current = nextAppState;
};
const subscription = AppState.addEventListener(
"change",
handleAppStateChange
);
return () => {
subscription.remove();
};
}, []);
그러나 권한을 "허용 안 함"으로 설정하자마자,
AppState.addEventListener
에 등록한 콜백함수가 무한으로 동작했다.
깃헙 답변을 확인하니 아래와 같았다.
@shomatz not sure if it helps but for me it was because I was requesting app permissions on Android and every time AppState changed I requested them again. But I wasn’t aware that AppState changes to background while Android is requesting a permission (not the behaviour on iOS).
Once I accounted for this, I fixed my infinite loop.
즉, Android가 권한을 요청하는 동안 AppState가 백그라운드로 변경된다..
하루종일 삽질했다..
해결 방법은 Android일 경우에 app으로 돌아와 버튼을 한 번 더 누르는 방식으로 해결했다.
android 13부터는 앱 내부에서 권한 띄우는 게 있다고 하던대 react-native에서 가능한지는 찾아봐야겠다.