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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형
Posted on . Written by



오늘의 guest tutorial 은 Ingemar Bergmark이 제공합니다. 그는 Corona Enterprise 개발자 입니다. 유닉스 세계에서 Informix와 Oracle을 사용한 DBA/Systems Analyst 경력을 갖고 있고 25년간 프로그래밍을 해 오고 있습니다. 그는 그의 첫번째 컴퓨터인 Sinclair ZX-80 와 더불어 프로그래밍의 세계에 흥미를 갖기 시작했습니다. 10대 초반에 게임 개발에 관심을 갖기 시작했었고 Hercules Graphics Card 가 나오고 부터는 그의 친구들을 위해  graphics SDKs 들을 만드는데 재미를 붙였었습니다.  최근에 그는 게임 개발 세계에 그의 venture 기업을 만듦으로서 발을 본격적으로 발을 들여 놨습니다. 그는 한국 시흥의 독립 스튜디오인 Swipeware의 owner 입니다. 그의 웹 사이트인 www.swipeware.com에 가시면 좀 더 자세한 내용을 보실 수 있습니다.

여러분 앱에서 직접 여러분의 custom extension을 가지고 이메일의 첨부를 열어 보고 싶지 않으세요? 오늘의 튜토리얼은 어떻게 iOS build settings 을 하고 여러분 앱의 documents 디렉토리에 파일을 보내고 그 파일의 path를 구하기 위해 launchArgs URL을 사용하는 방법을 코로나로 어떻게 하는지에 대해 설명 드리겠습니다.





1. Register the Extension in build.settings



첫번째로 여러분의 extension을 build.settings 의 plist section 에 register 시켜야 합니다. 그 안에 두개의 table들이 있게 될 텐데요. 그 테이블 안에는 여러 파라미터들이 들어가게 될 겁니다.

    CFBundleDocumentTypes 은 여러분의  custom extension 을 정의합니다.
    UTExportedTypeDeclarations tells iOS about your custom extension so that other apps (like Mail) will present the option to open the file in your app.
    UTExportedTypeDeclarations는 iOS 에게 여러분의 custom extension에 대해 알려 줍니다. so that other apps (like Mail) will present the option to open the file in your app.

아래 예제에서는 “wxyz“ 라는 custom file extension을 register 합니다. build.settings modifications를 보세요.


settings =
{
orientation =
{
default = "portrait",
supported = { "portrait" }
},
iphone =
{
plist =
{
UIStatusBarHidden = false,
UIPrerenderedIcon = true,
UIApplicationExitsOnSuspend = false,
CFBundleDocumentTypes =
{
{
CFBundleTypeIconFiles =
{
"doctype-hires.png",
"doctype.png"
},
CFBundleTypeName = "wxyz File",
CFBundleTypeRole = "Viewer",
LSHandlerRank = "Owner",
LSItemContentTypes =
{ "com.mycompany.myapp.wxyz" }
}
},
UTExportedTypeDeclarations =
{
{
UTTypeConformsTo =
{
"public.plain-text",
"public.text"
},
UTTypeDescription = "wxyz File",
UTTypeIdentifier = "com.mycompany.myapp.wxyz",
UTTypeTagSpecification =
{
["public.filename-extension"] = "wxyz",
["public.mime-type"] = "myapp/wxyz"
}
}
}
}
}
}



CFBundleDocumentTypes table과 그 안의 파라미터들을 한번 살펴 보죠.

    CFBundleTypeIconFiles — document icon으로 사용 될 이미지 파일의 이름을 담고 있는 string의 배열. 이 이미지들의 사이즈와 specification에 대해 좀 더 자세히 알고 싶으시면 여기에서 Apple의 가이드라인을 확인하세요.
    CFBundleTypeName — document type 에 대한 "abstract" 이름, type을 표현함. key 가 있어야 함. 예를 들어 위 예제에서는 “wxyz File“ 을 사용하고 있음
    CFBundleTypeRole — 그 type에 대한 앱의 role 을 설정함. 이 값은 Editor, Viewer, Shell 이 되거나 아무것도 안 올 수 있음. 이 key 는 필요하고 우리는 이것을 Viewer 로 설정할 것임.
    LSHandlerRank — 이 타입을 사용하는 파일들이 어떤 성격의 것들인지 알리기 위해 사용합니다. 사용할 수 있는 값으로는 Owner, Alternate, None 등이 있습니다.  예제에서는 Owner 를 사용합니다. 이 앱이 이 타입의 파일을 생성하는 앱이라는 것을 나타내기 위해서요.

    LSItemContentTypes — an array of strings; each one must contain a UTI (Uniform Type Identifier) defining a supported file type. For a detailed description of UTI concepts, please refer to Apple’s guidelines here. In this case, we specify the app’s bundle identifier plus our desired file extension of .wxyz.
    LSItemContentTypes - string 으로 구성된 배열. 각각은 지원되는 파일 타입을 정의하는 UTI (Uniform Type Identifier) 를 반드시 가지고 있어야 함. UTI 에 대한 좀 더 자세한 내용은 애플의 가이드라인을 참조하세요. 우리는 앱의 bundle identifier 와 우리가 사용할 팡리 확장자(.wxyz)를 지정했습니다.



이제 UTExportedTypeDeclarations table과 파라미터들을 살펴 보겠습니다.(complete reference can be found here):


    UTTypeConformsTo - string 들로 구성된 배열. 각 string 은 해당 타입을 confirm 하는 UTI 를 정의하고 있습니다. 이 것들은 custom 파일 포맷이 속한 parent 카테고리들을 표현합니다. 예를 들어 JPEG 파일은 public.image 와 public.data 타입임을 confirm 합니다. 좀 더 자세한 내용은 애플의 문서를 참조하세요.
    UTTypeDescription — An “abstract” user-readable description of this type. 유저가 읽을 수 있는 이 타입의 description. (abstract)
    UTTypeIdentifier — 이 타입과 관련된 UTI. 여기서는 앱의 번들 identifier 와 우리가 사용할 파일 확장자를 가리킴
    UTTypeTagSpecification — 한개 이상의 equivalent type idenrifier 를 정의한 dictionary. key-value 한쌍의 형식으로 이뤄졌음. 이 타입의 파일 이름 확장자, MIME 타입, OSType 코드 그리고 pasteboard type 등이 있음.

   


2. Build Handler Functions

다음으로 여러분 앱에 handler 기능을 build 해야 합니다. 아래 코드들을 잘 살펴 보세요.


-----------------------------------------------------------------------------------------
-- main.lua
-----------------------------------------------------------------------------------------
local launchArgs = ...
local launchFile = "";
local fileName;
 
 
local getDocPath = function(fName)
local f = fName;
local docStart, docEnd = f:find("Documents");
f = f:sub(docEnd+1);
return f;
end
 
 
if (launchArgs and launchArgs.url) then
launchFile = launchArgs.url;
if (string.sub(launchFile, 1, 7) == "file://") then -- handle custom extension
launchFile = getDocPath(launchFile);
else
-- handle URL Scheme / do something
end
end
 
 
-- text for testing
display.newText("File passed to system.DocumentsDirectory:", 10, 50, native.systemFont, 12);
fileName = display.newText(launchFile, 10, 65, native.systemFont, 12);
 
 
local onSystemEvent = function(event)
local eventType = event.type;
local eventURL = event.url;
if (eventType == "applicationOpen") then -- app resumes from background
if (eventURL) then
launchFile = getDocPath(eventURL);
fileName.text = launchFile;
end
end
end
Runtime:addEventListener("system", onSystemEvent);


위 예제는 아주 쌈박한 예제는 아닙니다. 단지 Documents 디렉토리안의 imported file 에 대한 path만 display 합니다. 좀 자세히 살펴 볼까요?

    First, we use … which is Lua syntax that returns a table with arguments passed to your app by the system.
    Following that is a utility function that extracts and returns the path to your file in the Documents directory.
    Next, we check for launch arguments. If the .url passed to the app begins with “file://“, we know that we’re trying to handle a custom extension. The .url part can also be used to handle URL schemes, but that’s explained in a different tutorial here.
    Two display objects are then created for the purpose of showing the path to the file that was imported into the app.
    Since our app can suspend to the background, we need a way to detect when the app resumes with an attachment. This is done by adding an event listener for the system event. The event type we look for is applicationOpen.


위의 코드들을 implement 한 후 모든 setting들을 꼼꼼히 검토해 보세요. 그리고 나서 wxyz 확장자를 갖는 첨부파일을 넣고 여러분에게 이메일을 보내 보세요.


이제 여러분의 device 에서 Mail app 을 여시고 방금 보낸 그 메일을 선택하세요. 그리고 첨부파일을 “tap-and-hold” 하세요. 이제 “Open in” dialog pop-up에서 여러분의 앱 이름을 보실 수 있을 겁니다. 그 리스트에서 여러분 앱을 선택하시면 여러분 앱이 Document directory 안의 파일과 함께 다음 프로세스를 진행하기 위해 열릴겁니다.



What’s Next?


그 다음 작업으로 저는 다른 사람들로부터 받은 데이터(첨부)를 import 할 수 있도록 하고 그 데이터를 parse 한 다음에 SQLite 데이터 베이스에 저장하는 기능을 구현하고 있습니다. 그 기능이 완성되면 사람들이 첨부로 보낸 데이터를 쉽게 여러분 앱에 import 할 수 있도록 하는 기능을 제공하게 될 겁니다. 그 기능이 완료 될 때까지 이 튜토리얼이 코로나에서 이메일 첨부파일에 접근하고 그것을 share 하는 개념을 이해하는데 도움이 되기를 바랍니다.


반응형