iOS Tutorial: Using email attachments in Corona SDK

Posted on Feb 7, 2013 | 0 comments

Would you like to be able to open an email attachment with your own custom extension directly in your app? The following will show how you can do it.

First, you’ll need to register your extension in the plist section of your build.settings.
In this example I’m registering a custom file extension ‘wxyz’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- build.settings
 
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 defines your custom extension.
UTExportedTypeDeclarations tells iOS about your custom extension so that other apps (like Mail) can see it and give an option to open the file in your app.

The file is copied by the system to the app’s system.DocumentsDirectory, where your app can process it any way you want.

You use launchArgs in your main.lua to get the filename.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-----------------------------------------------------------------------------------------
--
-- 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
 
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);

The sample code above doesn’t do anything fancy. It simply displays the path to your imported file in the Documents directory. Let’s have a look at what’s happening:

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.

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“.

After you’ve double-checked everything, send yourself an email with an attachment that has a ‘wxyz’ extension.
Open the Mail app and select your email. “Tap-and-hold” the attachment. You should see your app in the “Open in” dialog that pops up. Select your app from the list and voila! The file is in your Documents directory for further processing.

Here’s a link to Apple’s documentation that further explains the keys used in the plist above.

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha Captcha Reload

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>