PhoneGap-1.7.0(Cordova) Xcode 4.2 iOS Tutorial Example
PhoneGap-1.7.0(Cordova) 사이트에서 제공 하는 다음과 같은 내용을 제작 하고 테스트 할 것이다.
제작 되어 보여 지는 내용은 다음 이미지와 같다.
위의 이미지는 PhoneGap-1.7.0(Cordova) 에서 제공 하는 기능은 전체 들어간 예제이다. 이 번 실습에서는 단순히 테스트용으로만 제작을 할 것이며, 각각의 기능에 대한 상세한 설명은 하지 않을 것이다.
하이브리드 앱을 제작 하고자 하는 많은 분들께, 간단한 테스트로서 기능만을 맛 보기 위해서 하는 작업이다.
작업은 Xcode 4.2 와 PhoneGap-1.7.0(Cordova) 를 가지고 할 것이지만 실제 테스트는 폰으로 연결 하여 잘 실행이 되는지를 직접 확인 하시기를 바란다.
실제 전체 소스는 제공 되지 않지만 안드로이드용의 소스를 가지고 Xcode에 포팅 하는 과정으로 보시는 것이 더 좋을 것이다.
PhoneGap-1.7.0(Cordova) 을 사용한 하이브리드 앱의 가능성을 개인의 판단으로 미루고 본격적으로 준비 및 코딩과 실행을 해 보도록 합시다.
- Xcode 4.2 에서 PhoneGap-1.7.0(Cordova) 의 Example 프로젝트 셋팅.
- 이 전에 Xcode 4.2 에 PhoneGap-1.7.0(Cordova)를 설치 하고 간단한 프로젝트를 하난 만들어 봤기 때문에 설치 과정은 하지 않기로 한다.
- 먼저 Xcode를 실행 하자.
- 다음과 같이 프로젝트를 생성 한다.
- 위의 이미지에서 Next 를 클릭 하면 다음과 같은 화면에서 프로젝트명과 패키지명등을 설정한다.
: Project Name - CordovaExample
: Comapny Identifier - com.cordova.example
: Use Automatic Reference Counting 은 체크 하지 않는다.
그리고 Next 버튼을 클릭 하여 다음 단계로 넘어 간다.
- 다음과 같은 화면이 나오면 왼쪽의 New Folder 버튼을 클릭 한다.
- 다음과 같은 화면에서 CordovaExample 이라고 폴더를 입력 하고 Create 버튼을 클릭 한다.
- 그리고 마지막으로 나오는 다음 화면에서 Create 버튼을 클릭 하여 프로젝트 생성에 필요한 모든 과정을 마무리 한다. - Xcode 4.2 프로젝트내 PhoneGap-1.7.0(Cordova)를 사용 하기 위한 준비.
- 우선 앱을 테스트 하기 위한 시뮬레이터를 먼저 선택 하자.
- 먼저 실행을 먼저 해 보자.
이렇게 먼저 하는 이유는 하이브리드 앱을 개발 하기 위해서 필요한 것이 www 폴더 인데, 지금은 그 www 폴더가 없다. www 폴더를 만들어 내기 위해서 Run 을 클릭 하여 www 폴더가 내부적으로 자동으로 생성 되게 하기 위한 것이다.
물론 index.html 파일과 cordova-1.7.0.js 자바스크립트 파일도 같이 생성이 된다.
- 다른 하이브리드 앱을 제작 한다고 하면, 지금 하는 과정은 필히 빠트리지 않도록 주의 합시다.
- 자, 이번에는 위에서 말한 www 폴더가 자동으로 생성이 되었는지 확인도 하고 프로젝트에 www 폴더를 추가 하는 작업을 하도록 합시다.
다음 이미지와 같이 해 보자. 상단의 프로젝트를 마우스로 오른쪽 버튼 클릭 하면 Show in Finder 메뉴가 나올 것이다 그 것을 클릭해 보자.
팝업창이 하나 뜰 것이다. 거기에 www 폴더를 마우스로 끌어서 다음 이미지와 같이 프로젝트에 추가를 한다.
그리고 나오는 팝업창에서 다음 그림과 같이 선택을 한 다음 Finish 버튼을 클릭 하여 프로젝트에 추가 하는 작업을 마무리 한다. - PhoneGap-1.7.0(Cordova) 에서 제공 하는 Example 코딩.
- 위에서 완료된 작업의 구조를 보면 다음 이미지와 같이 나온다.
- 지금 바로 Run 을 클릭 하여 바로 테스트를 해 보자. 추가한 내용이 잘 되는지만 확인을 하면 된다. 다음 이미지와 같이 나오면 정상적으로 지금까지 작업이 잘 된 것이다.
- www 폴더에서 마우스로 오른쪽 버튼을 클릭 하여 New File... 를 클릭 한다.
다음과 같은 팝업창에서 선택을 하고 클릭을 하면 파일이 자동으로 생성 된다.
이와 같은 방법으로 프로젝트에 사용할 파일을 몇 개 생성을 하자.
: master.css, main.js 2개의 파일.
- 생성한 다음의 파일 구조는 다음 이미지와 같다.
- 자, 이제는 파일 하나씩 코딩을 해 보도록 합시다.
우선 index.html 파일을 다음과 같이 수정을 할 것이다. 기존에 있던 내용은 모두 삭제 하고 입력을 한다.
<!DOCTYPE HTML> <html> <head> <meta name="viewport" content="width=320; user-scalable=no" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>PhoneGap</title> <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title"> <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script> <script type="text/javascript" charset="utf-8" src="main.js"></script> </head> <body onload="init();" id="stage" class="theme"> <h1>Welcome to Cordova!</h1> <h2>this file is located at assets/www/index.html</h2> <div id="info"> <h4>Platform: <span id="platform"> </span>, Version: <span id="version"> </span></h4> <h4>UUID: <span id="uuid"> </span>, Name: <span id="name"> </span></h4> <h4>Width: <span id="width"> </span>, Height: <span id="height"> </span>, Color Depth: <span id="colorDepth"></span></h4> </div> <dl id="accel-data"> <dt>X:</dt><dd id="x"> </dd> <dt>Y:</dt><dd id="y"> </dd> <dt>Z:</dt><dd id="z"> </dd> </dl> <a href="#" class="btn large" onclick="toggleAccel();">Toggle Accelerometer</a> <a href="#" class="btn large" onclick="getLocation();">Get Location</a> <a href="tel:411" class="btn large">Call 411</a> <a href="#" class="btn large" onclick="beep();">Beep</a> <a href="#" class="btn large" onclick="vibrate();">Vibrate</a> <a href="#" class="btn large" onclick="show_pic();">Get a Picture</a> <a href="#" class="btn large" onclick="get_contacts();return false;">Get Phone's Contacts</a> <a href="#" class="btn large" onclick="check_network();return false;">Check Network</a> <dl> <dt>Compass Heading:</dt><dd id="h">Off</dd> </dl> <a href="#" class="btn large" onclick="toggleCompass();return false;">Toggle Compass</a> <div id="viewport" class="viewport" style="display: none;"> <img style="width:60px;height:60px" id="test_img" src="" /> </div> </body> </html> |
이 번에는 master.css 파일을 다음과 같은 내용으로 코딩을 한다.
body { background:#222 none repeat scroll 0 0; color:#666; font-family:Helvetica; font-size:72%; line-height:1.5em; margin:0; border-top:1px solid #393939; } #info{ background:#ffa; border: 1px solid #ffd324; -webkit-border-radius: 5px; border-radius: 5px; clear:both; margin:15px 6px 0; width:295px; padding:4px 0px 2px 10px; }
#info > h4{ font-size:.95em; margin:5px 0; }
#stage.theme{ padding-top:3px; } /* Definition List */ #stage.theme > dl{ padding-top:10px; clear:both; margin:0; list-style-type:none; padding-left:10px; overflow:auto; } #stage.theme > dl > dt{ font-weight:bold; float:left; margin-left:5px; } #stage.theme > dl > dd{ width:45px; float:left; color:#a87; font-weight:bold; } /* Content Styling */ #stage.theme > h1, #stage.theme > h2, #stage.theme > p{ margin:1em 0 .5em 13px; } #stage.theme > h1{ color:#eee; font-size:1.6em; text-align:center; margin:0; margin-top:15px; padding:0; } #stage.theme > h2{ clear:both; margin:0; padding:3px; font-size:1em; text-align:center; } /* Stage Buttons */ #stage.theme a.btn{ border: 1px solid #555; -webkit-border-radius: 5px; border-radius: 5px; text-align:center; display:block; float:left; background:#444; width:150px; color:#9ab; font-size:1.1em; text-decoration:none; padding:1.2em 0; margin:3px 0px 3px 5px; } #stage.theme a.btn.large{ width:308px; padding:1.2em 0; } |
마지막으로 main.js 파일을 코딩 한다.
var deviceInfo = function() { document.getElementById("platform").innerHTML = device.platform; document.getElementById("version").innerHTML = device.version; document.getElementById("uuid").innerHTML = device.uuid; document.getElementById("name").innerHTML = device.name; document.getElementById("width").innerHTML = screen.width; document.getElementById("height").innerHTML = screen.height; document.getElementById("colorDepth").innerHTML = screen.colorDepth; }; var getLocation = function() { var suc = function(p) { alert(p.coords.latitude + " " + p.coords.longitude); }; var locFail = function() { }; navigator.geolocation.getCurrentPosition(suc, locFail); }; var beep = function() { navigator.notification.beep(2); }; var vibrate = function() { navigator.notification.vibrate(0); }; function roundNumber(num) { var dec = 3; var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); return result; } var accelerationWatch = null; function updateAcceleration(a) { document.getElementById('x').innerHTML = roundNumber(a.x); document.getElementById('y').innerHTML = roundNumber(a.y); document.getElementById('z').innerHTML = roundNumber(a.z); } var toggleAccel = function() { if (accelerationWatch !== null) { navigator.accelerometer.clearWatch(accelerationWatch); updateAcceleration({ x : "", y : "", z : "" }); accelerationWatch = null; } else { var options = {}; options.frequency = 1000; accelerationWatch = navigator.accelerometer.watchAcceleration( updateAcceleration, function(ex) { alert("accel fail (" + ex.name + ": " + ex.message + ")"); }, options); } }; var preventBehavior = function(e) { e.preventDefault(); }; function dump_pic(data) { var viewport = document.getElementById('viewport'); console.log(data); viewport.style.display = ""; viewport.style.position = "absolute"; viewport.style.top = "10px"; viewport.style.left = "10px"; document.getElementById("test_img").src = "data:image/jpeg;base64," + data; } function fail(msg) { alert(msg); } function show_pic() { navigator.camera.getPicture(dump_pic, fail, { quality : 50 }); } function close() { var viewport = document.getElementById('viewport'); viewport.style.position = "relative"; viewport.style.display = "none"; } function contacts_success(contacts) { alert(contacts.length + ' contacts returned.' + (contacts[2] && contacts[2].name ? (' Third contact is ' + contacts[2].name.formatted) : '')); } function get_contacts() { var obj = new ContactFindOptions(); obj.filter = ""; obj.multiple = true; navigator.contacts.find( [ "displayName", "name" ], contacts_success, fail, obj); } function check_network() { var networkState = navigator.network.connection.type; var states = {}; states[Connection.UNKNOWN] = 'Unknown connection'; states[Connection.ETHERNET] = 'Ethernet connection'; states[Connection.WIFI] = 'WiFi connection'; states[Connection.CELL_2G] = 'Cell 2G connection'; states[Connection.CELL_3G] = 'Cell 3G connection'; states[Connection.CELL_4G] = 'Cell 4G connection'; states[Connection.NONE] = 'No network connection'; confirm('Connection type:\n ' + states[networkState]); } var watchID = null; function updateHeading(h) { document.getElementById('h').innerHTML = h.magneticHeading; } function toggleCompass() { if (watchID !== null) { navigator.compass.clearWatch(watchID); watchID = null; updateHeading({ magneticHeading : "Off"}); } else { var options = { frequency: 1000 }; watchID = navigator.compass.watchHeading(updateHeading, function(e) { alert('Compass Error: ' + e.code); }, options); } } function init() { // the next line makes it impossible to see Contacts on the HTC Evo since it // doesn't have a scroll button // document.addEventListener("touchmove", preventBehavior, false); document.addEventListener("deviceready", deviceInfo, true); } |
- 전체적으로 테스트에 필요한 모든 작업은 끝이 났다. 지금까지 한 내용이 잘 되는지를 Run 을 클릭 하여 실제 폰에서 테스트 해 보자.
- 실제 폰에서의 실행된 화면들의 캡쳐이다.
- 다음 시간에는 Cordova 에서 제공 하는 각 함수를 천천히 살펴 보기로 하자.
- 다음 내용은 Cordova에서 제공 하는 기능들이다.
- Accelerometer
- Camera
- Capture
- Compass
- Connection
- Contacts
- Device
- Events
- File
- Geolocation
- Media
- Notification
- Storage
프로필. 부서 : 개발팀 직위: 팀장 이름 : 안병도(꼬장) 메일 : yaio@naver.com, 네이버카페 : http://cafe.naver.com/mobilewebapp 트위터 : @yaioyaio 페이스북 : http://www.facebook.com/byoungdo.ahn 워드프레스: http://yaioyaio.wordpress.com/ |
'PhoneGap' 카테고리의 다른 글
PhoneGap-1.7.0(Cordova) XCode 4.2 iOS Tutorial 기본 사용법 (0) | 2012.05.13 |
---|---|
PhoneGap-1.7.0(Cordova) Eclipse Helios Mac용 Android Tutorial Example (0) | 2012.05.13 |
PhoneGap-1.7.0(Cordova) Eclipse Helios Mac용 Android Tutorial 기본 사용법 (0) | 2012.05.13 |