기본 콘텐츠로 건너뛰기

Meteor 1.0 - Android/iOS 앱을 만들기 위해 Cordova plugin을 사용해보자

Camera와 Gallery를 사용하는 앱을 Meteor로 만들자.

준비
meteor 설치 (https://www.meteor.com/install)

실행과정 :

project를 생성하고
$ meteor create camera-practice
$ cd camera-practice

iOS, android 환경을 추가한다.

$ meteor add-platform ios
$ meteor add-platform android

You must agree to the terms to proceed.       
Do you agree (Y/n)? 
에서 Y혹은 enter
android: added platform 

플랫폼 추가 완료.

플러그인 추가
$ meteor add cordova:org.apache.cordova.camera
Must declare exact version of dependency: org.apache.cordova.camera@undefined

에서 버전 정보를 확인해보니 0.3.4-dev 하지만 역시 빌드시 오류가 있으므로 안전한 0.3.2로 가자.

$ meteor add cordova:org.apache.cordova.camera@0.3.2
added cordova plugin org.apache.cordova.camera@0.3.2

성공. cordova라는 namespace만 붙여주면 마치 원래 Meteor package인양 가져오는 모습이 아주 흐뭇하다 :)

나는 coffeescript를 좋아하니까

meteor add cofffeescript

일단 필요없는 초기파일 html, css, js들은 다 지우고
client 디렉토리를 하나 만든 후 html과 coffee 파일을 만들자
rm *
mkdir client
cd client
touch getPicture.html
touch getPicture.coffee

getPicture.html
<head>
  <title>cordova-meteor-practice-camera</title>
  <meta name="viewport" content="width=device-width">
</head>
<body>
  <h1>Camera Demo</h1>
  {{> getPicture}}
</body>

<template name="getPicture">
  <button id="album">from Album</button>
  {{#if photo}}
  <div>
    <img src="{{photo}}">
  </div>
  {{/if}}
</template>

getPicture.coffee
Template.getPicture.helpers
  photo: ->
    Session.get 'photo'
Template.getPicture.events
  'click #album': ->
    navigator.camera.getPicture (data)->
      Session.set 'photo', "data:image/jpeg;base64,#{data}"
    ,
      (error)->
        new Meteor.Error "cordovaError", error
    ,
      destinationType: Camera.DestinationType.DATA_URL
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY

coffee 내용을 잘 모르겠다면 http://js2coffee.org 에서 복붙하여 변환해보자.
노파심에 말하지만 오른쪽에 붙여야한다 :)

getPicture template에서 album이라는 id를 가진 button을 click했을 때 event를 받아
navigator.camera.getPicture 를 실행하여 사진엘범의 사진을 가져와 DATA_URL형태로 받은 것을 photo라는 Session 값으로 넘겨준다.
그러면 Reactive 객체인 photo는 같은 이름의 helper인 photo로 img에 들어가는 구조이다.

$ meteor run ios
   Installing Cordova plugins...             [=============             ] 50% 31.1s

플러긴 한참 설치하고 빌드하고 처음 시작을 준비한다.

=> Started proxy.                             
=> Started MongoDB.                           
=> Started app on iOS Simulator.              
=> Started your app.                          

=> App running at: http://localhost:3000/

이렇게 나오고 ios simulator를 확인해본다.
디버깅을 하려면 Develop > iOS Simulator > meteor.local 을 선택해보면 된다.

console에서 device나 cordova, navigator, Camera 객체 등등의 내용을 확인해보면 흥미로울 것이다.

iOS Simulator에서 이쁜 기본 Meteor splash screen과 함께 앱이 실행된다.
Simulator라 내장 카메라 작동이 안되므로 Album을 구현하였다.
화면이 나오면
버튼을 눌러보자.
사진을 열람하게 할거냐고 권한을 물어본 뒤 사진을 선택할 수 있다.
 선택하면 버튼 하단에 사진이 나오는 것을 확인할 수 있다.
camera plugin에 대한 자세한 내용은 https://github.com/apache/cordova-plugin-camera/blob/master/doc/index.md 를 참조하자.

폰을 연결하고 meteor run ios-device 로 실행하면 카메라도 테스트해볼 수 있다.

  <button id="camera">from camera</button>
버튼을 하나 더 만들고

Template.getPicture.events 아래에
  'click #camera': ->
    navigator.camera.getPicture (data)->
      Session.set 'photo', "data:image/jpeg;base64,#{data}"
    ,
      (error)->
        new Meteor.Error "cordovaError", error
    ,
      destinationType: Camera.DestinationType.DATA_URL
      sourceType: Camera.PictureSourceType.CAMERA

이 부분을 추가해보자.
사실 sourceType은 default가 CAMERA이므로 생략해도 무방하다.
놀라운 점이 하나 있다면 다시 앱을 재실행하지 않아도 부드럽게 적용이 된다.
이것은 반복적인 UI 확인/테스트를 요하는 모바일 앱 작성에 있어 강력한 장점이다.


개인적인 감상으론 Hot Code Push를 비롯하여 깔끔한 파일구조등등을 고려했을때 그냥 서버없는 cordova app을 만든다고 해도 Meteor를 추천하고 싶은 심정이다.

게다가 coffeescript 를 쓰기 위해 멍청한 javascript task runner류들을 만들지 않아도 되는 것이 너무 좋다.
grunt? gulp? jake? 잊어라. 그리고 Meteor를 하자.

댓글

이 블로그의 인기 게시물

세상 간단한 https(+secured websocket): Caddy

nginx, apache2 같은 걸로 매번 certbot 연동을 통해 https 하는게 지겨워서 알아보니 Caddy라는게 있더라. 설치법은 강하게 크기 위해 알아서 해본다. # cat /etc/caddy/Caddyfile your.shitty.site:8123 {   proxy / localhost:9123 {     websocket     transparent   } } 이건 외부에서 8123으로 들어오는 걸 내부적으로 9123로 맞춰서 https 맞춰주는 reverse proxy. 만일 port를 생략하면 # cat /etc/caddy/Caddyfile your.shitty.site {   proxy / localhost:9123 {     websocket     transparent   } } 기본포트인 443으로 되어 https://your.shitty.site 로 접속이 된다. 요새 말썽이던 cloudflare 버리고 netlify domain이랑 caddy를 쓰니까 앓던 이가 빠진 느낌. 이 조합은 당분간 계속 써야겠다 싶다.

저지연 오디오 생성 LLVC 사용기

https://github.com/KoeAI/LLVC 써봄 CUDA 11.8 버전 쓰고 있어 pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 VC 없어서 설치 https://wikidocs.net/168848 참조 FAIRSEQ 관련 오류 https://github.com/facebookresearch/fairseq/issues/2908 pip install editdistance  설치 No CUDA runtime is found, using CUDA_HOME='C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8' 오류 멀쩡하게 있는데 안됨. pip install torch==2.1.0+cu118 torchvision==0.16.0+cu118 torchaudio==2.1.0+cu118 --extra-index-url https://download.pytorch.org/whl/cu118 다시 설치 set DISTUTILS_USE_SDK=1 이게 문젠가 설정하고 재도전 Using cached ruamel.yaml.clib-0.2.8-cp311-cp311-win_amd64.whl (118 kB) Building wheels for collected packages: fairseq   Building wheel for fairseq (pyproject.toml) ... error   error: subprocess-exited-with-error   × Building wheel for fairseq (pyproject.toml) did not run successfully.   │ exit code: 1   ╰─> [1829 lines of output]       No CUDA runtime is found, usi...