반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

수요일의 FAQ 5 번째 시간

2012. 5. 21. 06:58 | Posted by 솔웅


반응형

FAQ Wednesday #5


Question 1

Lua File System(LFS)를 추가하셨죠? /Documents directory 에서 파일 리스트를 얻으려면 어떻게 해야하죠?

Answer

Lua File System은 Corona build 2012.805 에서부터 추가됐습니다. 여기에서 튜토리얼을 읽어보실 수 있습니다.

LFS는 subdirectory도 만들고 지우고 접근할 수 있게 됐습니다. 구현은 디렉토리의 path를 담은 string을 만드는게 key 입니다. iOS와 Android에서는 3개의 main directory들이 있습니다. Resource, Temporary 그리고 Documents 가 그것입니다. 이것들은 Corona constant들을 사용해서 접근하게 됩니다. userdata constants들은 LFS에서 사용될 수  있으려면 string으로 변환 될 필요가 있습니다. system.pathForFile 는 userdata constant 로 변환 됩니다. (string 타입으로). 여러분은 base directory와 함께 파일이나 디렉토리명을 명시해주시면 됩니다. 그러면 그것은 전체 경로를 return 하게 됩니다.

local path = system.pathForFile( "data", system.DocumentsDirectory )

위 코드는 다음과 같은 string을 return 할 겁니다. “/var/mobile/Applications//Documents/data”



아래에 file들 리스트를 LFS로 어떻게 출력되게 하는지에 대한 코드가 있습니다.


local lfs = require "lfs"

-- Print the files in the path
--
local function printDir( path )
    print( "\nFiles in path: " .. tostring( path ) )
 
    local pathType = ""
 
    -- Check to see if path exists
    if path and lfs.attributes( path ) then
        pathType = lfs.attributes( path ).mode
    end
 
    if pathType == "directory" then
        for file in lfs.dir( path ) do
            local str

            -- Skip the current and up-one directories
            if "." ~= file and ".." ~= file then
                str = file

                local fileAtr = lfs.attributes( path .. "/" .. file )
                if fileAtr then
                    str = str .. " --> (" .. fileAtr.mode .. ")"
                else
                    str = str .. " --> (none)"
                end

                print( str )
            end
        end
    else
        str = "Path is not a directory!"
        print( str )
    end
end


그리고 아래는 /Documents directory와 /Documents/data subdirectory 를 어떻게 불러서 출력하는지를 보여 줍니다. (만약 존재한다면)

path = system.pathForFile( nil, system.DocumentsDirectory)
printDir( path )
path = system.pathForFile( data, system.DocumentsDirectory)
printDir( path )


위 코드는 파일 이름들을 출력하는 대신 table(배열)에 담아서 활용할 수도 있을 겁니다.


Question 2


Resource 디렉토리의 파일에 접근을 시도할 때 warning과 error를 하나씩 받았습니다. 이거 버그인가요?


Answer

system.pathForFile 를 사용하고 베이스 디렉토리가 system.ResourceDirectory 일때  코로나는 file이 존재하는지 살펴보고 그 path에 대해 nil 값이 받아지면 warning 메세지를 발생시킵니다. 그런데 이것은 filename 파라미터가 실제로 디렉토리 이름이면 작동하지 않습니다. 이것을 이용해서 다음과 같은 트릭을 쓰기도 합니다. 

local path = system.pathForFile( nil, system.ResourceDirectory )


이렇게 하면 warning 메세지를 발생하지 않습니다. 그리고 Resource directory를 가리키는 string을 return 하죠. 이 Resource directory 밑에 subdirectory 의 경로를 생성할 필요가 있으면 다음과 같이 이것을 append 하시면 됩니다.

path = system.pathForFile( nil, system.ResourceDirectory)
printDir( path )
path = system.pathForFile( nil, system.ResourceDirectory) .."/".."data"
printDir( path )

Note : system.ResourceDirectory는 안드로이드에서는 Resource directory가 실제 file system의 일부분이 아니기 때문에 작동하지 않습니다.

그리고 Resource directory는 read-only 디렉토리라서 write 할 수 없다는 것도 기억하세요.


Question 3

single touch app에 Multitouch 세팅하기

Answer

이건 질문은 아니지만 하나의 tip으로서 알려드립니다. 코로나에서 디폴트는 multitouch off 입니다. 이 말은 touch listener 가 있다면 한번에 하나의 touch 이벤트를 받는 다는 말입니다. 만약에 유저가 스크린을 터치하고 있는데 다른 손가락으로 버튼을 tap 한다거나 다른 객체를 move 하면 아무 일도 일어나지 않습니다. 왜냐하면 이미 다른 touch 가 active 돼 있기 때문이죠. 이것은 landscape 모드에서 유저가 손가락을 스크린에 대고 있느라고 자주 발생할 수 있는 현상이죠.

해결 방법은 여러분 코드에 아래와 같이 구현하는 겁니다.

system.activate( "multitouch" )

이렇게 하면 스크린에 손가락으로 하는 모든 touch에 touch event를 발생할 겁니다. touch 리스너를 걸어놓은 객체를 터치하지 않는 이상 아무일도 발생하지 않을 겁니다. 그리고 리스너가 있는 객체를 터치하면 동작이 일어나구요. 그렇게 되면 좀 더 user friendly 한 앱이 될 겁니다.


Question 4

iOS에서 앱 아이콘의 badge number를 어떻게 clear 하게 할 수 있을까요?


Answer


Local Notification은 해당 Notification이 일어나면 앱의 아이콘에 badge number를 세팅할 겁니다.(그것은 뭔가 pending 된게 있다는 것을 알리기 위함이죠.) 하지만 이것을 어떻게 clear 시킬 수 있을 까요? 해답은 native.setProperty API에 있습니다.

native.setProperty( "ApplicationBadgeIconNumber", 0 )

이렇게 하면 앱 아이콘에 있는 badge number를 없앨 수 있습니다.

아래 badge를 clears 시키는 Local Notification code 예제가 있습니다.

display.newText( "Setting Local Notification ...", 10, 30 )

-- Options for iOS
local options = {
   alert = "Wake up!",
   badge = 1,
   sound = "alarm.caf",
   custom = { foo = "bar" }
}

-- schedule using UTC (Coordinated Universal Time) time + 60 seconds
local utcTime = os.date( "!*t", os.time() + 60 )
local notification = system.scheduleNotification( utcTime, options )

-- Local Notification listener
local notificationListener = function( event )
   display.newText( "Notification Received " ..  event.badge, 10, 70 )
   native.setProperty( "applicationIconBadgeNumber", 0 )
end

Runtime:addEventListener( "notification", notificationListener )

Note: badge number 가 증가하거나 감소하는것에 대해서 badge 관련된 부분의 documentation에 설명 돼 있습니다. 이것은 애플의 documentation에서 인용한 겁니다. 그리고 그 의미는 여러분이 코드 내에서 그 badge number를 증가시키거나 감소시킬 수 있다는 것이죠. 코드 내에서 여러분이 명시한 badge number는 icon 에 display 되게 됩니다. 이 badge number 를 0으로 세팅하면 badge를 clear(remove) 하게 됩니다.

Local이나 Push Notification은 아직까지 iOS에서만 가능합니다.

Question 5

유저가 스크린을 터치하지 않고 있으면 device가 sleep 모드로 가는데 제가 만든 게임을 하는 동안에는 그 현상이 안 일어났으면 좋겠습니다. 가능한가요?


Answer

Yes, and the solution is one of the “system” APIs that works on both iOS and Android (but not in the simulators).

가능합니다. system API를 이용하시면 iOS와 Android 모두에서 가능합니다. (시뮬레이터에서는 안 됩니다.)

system.setIdleTimer( false )  -- disable device sleep mode

idle timer를 false로 해서 디바이스가 sleep 모드로 가지 않도록 합니다. true로 가면 sleep 모드로 가게 됩니다. 디폴트는 true 입니다. 이것을 false로 하면 디바이스의 배터리가 더 빨리 닳을 수가 있다는 것을 감안해서 사용하시기 바랍니다.

여기까지가 오늘의 Question들 입니다. 여러분에게 유용한 정보가 되었기를 바랍니다.

감사합니다.


반응형