Onsen UI와 Cordova 전용 기능
Onsen UI는 하이브리드 앱 개발에 아주 좋습니다. 이 섹션에서는 Cordova 앱을 개발할 때 알아두면 좋은 몇 가지 함정(pitfall)을 다룹니다.
출처: 문서
본문
Cordova 전용 기능 (Cordova-specific Features)
Onsen UI는 하이브리드 앱 개발에 아주 좋습니다. 이 섹션에서는 Cordova 앱을 개발할 때 흔히 겪는 몇 가지 함정을 설명합니다.
'deviceready' 이벤트 처리하기
Cordova API는 deviceready 이벤트가 발생한 후에 호출할 수 있습니다. Onsen UI에서는 ons.ready() 함수를 사용해 이 이벤트를 식별할 수 있습니다.
ons.ready(function() {
// deviceready event is fired
// Call whatever Cordova APIs
});
기기 뒤로가기 버튼 (Device Back Button)
Android 기기의 경우, Cordova는 하드웨어 뒤로가기 버튼에서 backbutton 이벤트를 발생시킵니다. 이 이벤트는 Cordova가 발생시키므로, 앱에 cordova.js 파일(또는 Monaca의 경우 loader.js)이 포함되어 있어야 한다는 것을 이해하는 것이 중요합니다.
데스크톱 브라우저에서는 ESC 키를 눌러 backbutton 이벤트를 시뮬레이션할 수 있습니다.
Onsen UI는 일부 요소에 Android 뒤로가기 버튼에 대한 기본 동작이 있는 핸들러를 설정합니다:
-
다이얼로그(Dialogs): 취소 가능(cancelable)하면 다이얼로그를 닫습니다(Modal과 Toast 제외).
-
내비게이터(Navigator): 페이지 스택이 비어 있지 않으면 페이지를 pop합니다.
-
스플리터(Splitter): 메뉴가 열려 있으면 닫습니다.
-
조건이 충족되지 않으면 이 요소들은 부모 요소의 핸들러를 호출합니다. 마지막 전역 핸들러는 앱을 닫습니다.
이것이 많은 앱에서 원하는 동작일 수 있지만, Onsen UI는 더 나은 커스터마이징을 위해 이 핸들러를 수정할 수 있게 합니다. 앱의 전역 핸들러(앱 닫기)를 변경하려면 ons 객체가 몇 가지 메서드를 제공합니다:
// Disable or enable
ons.disableDeviceBackButtonHandler();
ons.enableDeviceBackButtonHandler();
// Set a new handler
ons.setDefaultDeviceBackButtonListener(function(event) {
ons.notification.confirm('Do you want to close the app?') // Ask for confirmation
.then(function(index) {
if (index === 1) { // OK button
navigator.app.exitApp(); // Close the app
}
});
});
이 외에도, 언급한 요소들은 ons-page 요소와 함께 자신의 핸들러를 수정하는 방법을 노출합니다:
var myNavigator = document.getElementById('my-navigator');
myNavigator.onDeviceBackButton.disable(); // Disables back button handler
myNavigator.onDeviceBackButton.enable(); // Enables back button handler
myNavigator.onDeviceBackButton.isEnabled(); // Returns true if enabled
myNavigator.onDeviceBackButton.destroy(); // Destroys the current handler
var page = myNavigator.topPage;
page.onDeviceBackButton = function(event) {
console.log('Hardware back button!');
event.callParentHandler(); // Calls the parent handler (navigator handler in this case)
};
이 핸들러에 제공되는 이벤트 객체에도 event.callParentHandler() 메서드가 포함되어 있습니다.
더 알아보기
- 앱 용량 줄이기 가이드에서 하이브리드 앱 크기를 최적화하는 팁을 확인해 보세요.
- 배포 가이드에서 Cordova 앱을 스토어에 출시하는 방법을 살펴보세요.