[iOS HowTo] Opening an email attachment with your app

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

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

-----------------------------------------------------------------------------------------  
--  
-- 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);  

After you’ve done this send yourself an email with 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.
http://tinyurl.com/bxzvypl [import]uid: 70847 topic_id: 35582 reply_id: 335582[/import]

Wow! Thank you so much for this Tutorial. [import]uid: 103182 topic_id: 35582 reply_id: 141421[/import]

Fantastic contribution.
[import]uid: 199310 topic_id: 35582 reply_id: 141427[/import]

Nice! Thank you for sharing!!

Naomi [import]uid: 67217 topic_id: 35582 reply_id: 141431[/import]

Awesome!!! Thanks I was trying to figure this one out a couple of weeks ago!!! Couldn’t figure out the "public.filename-extension" part of it. [import]uid: 10280 topic_id: 35582 reply_id: 141472[/import]

Wow! Thank you so much for this Tutorial. [import]uid: 103182 topic_id: 35582 reply_id: 141421[/import]

Fantastic contribution.
[import]uid: 199310 topic_id: 35582 reply_id: 141427[/import]

Nice! Thank you for sharing!!

Naomi [import]uid: 67217 topic_id: 35582 reply_id: 141431[/import]

Awesome!!! Thanks I was trying to figure this one out a couple of weeks ago!!! Couldn’t figure out the "public.filename-extension" part of it. [import]uid: 10280 topic_id: 35582 reply_id: 141472[/import]