방법: 앱의 내비게이션 바 항목 가져오기 및 설정하기

게시일 14. 10월 2024 작성자 Jan Bunk


일부 웹사이트에서는 앱의 내비게이션 바 항목을 동적으로 수정하는 기능이 유용할 수 있어요. 다음 JavaScript 함수를 사용하면 돼요.

JavaScript 함수 사용하기

다음의 executeWhenAppReady() 함수를 확인해 보세요. 앱 도우미 스크립트예요.. 이 함수는 앱이 준비되기 전이나 웹사이트가 일반 브라우저에서 로드되었을 때 웹사이트에서 앱과 상호작용을 시도하지 않도록 해줘요(ReferenceError, 함수가 정의되지 않음).

getNavigationBarItems

현재 표시되고 있는 모든 내비게이션 바 항목을 나타내는 객체 목록을 가져오려면 이 함수를 사용하세요.


<script>
    try {
        // returns a list of objects representing the navigation bar items
        let navigationBarItems = (await getNavigationBarItems())["navigationBarItems"];
    }
    catch (e) {
        // Can occur if:
        // - the app couldn't connect to the native code. Should be very unlikely.
        console.log(e);
    }
</script>
    

setNavigationBarItems

표시할 내비게이션 바 항목을 수정하려면 이 함수를 사용하세요.


<script>
    try {
        // example for a navigation bar item object
        let newNavigationBarItem = {
            label: "Contact",
            action: {
                urlToRedirectTo: "https://webtoapp.design/contact",
                javascriptToExecute: null,
                elementToClickSelector: null,
                isFavoriteAction: false,
                isShareAction: false,
                isOpenExternallyAction: false,
                isSettingsAction: false,
                isRateAction: false,
                isPastNotificationsAction: false,
            },
            icon: {
                name: "list"
            },
        };

        // get the currently displayed navigation bar items
        let allNavigationBarItems = (await getNavigationBarItems())["navigationBarItems"];

        // add the new navigation bar item to the list of navigation bar items
        allNavigationBarItems.push(newNavigationBarItem);

        // display the list of navigation bar items, including the new navigation bar item
        await setNavigationBarItems(allNavigationBarItems);
    }
    catch (e) {
        // Can occur if:
        // - the app couldn't connect to the native code. Should be very unlikely.
        console.log(e);
    }
</script>
    

앱 메뉴 수정하기

앱 메뉴를 동적으로 변경하는 방법과 관련된 개발자 문서 목록은 다음과 같아요. 앱 레이아웃에 해당하는 문서를 보고 있는지 확인하세요.