Implemented Uniform Type Identifiers and when emailed a single attachment, stringWithContentsOfURL
returns an NSString
with the contents of the file defined by the NSURL
.
When selecting a file from an email with a number of files attached, stringWithContentsOfURL
returns all files attached, with header information, as an NSString
.
Is there a way to open just the selected file?
Update:
You could parse the raw source and determine which file was selected from the NSURL, potentially problematic I think.
End Update
Code (url in both cases point to a single file). The file extension is *.defects
NSString *file = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
Result for mail with single attachment
"Defect Names"
"Test1 Area"
"Test2 Area"
"Test 3 Area"
"Test 4 Area"
"Extra"
Result for mail with multiple attachments
--Apple-Mail=_785C0CB9-CB2F-43BE-AD5F-D7DEF5F97EFC
Content-Disposition: attachment;
filename=Areas.defects
Content-Type: defects/x-defects;
x-unix-mode=0644;
name="Areas.defects"
Content-Transfer-Encoding: 7bit
"Area of Defect"
"Test1 Area"
"Test2 Area"
"Test 3 Area"
"Test 4 Area"
"Extra"
--Apple-Mail=_785C0CB9-CB2F-43BE-AD5F-D7DEF5F97EFC
Content-Disposition: attachment;
filename="pos.defects"
Content-Type: defects/x-defects;
x-unix-mode=0644;
name="pos.defects"
Content-Transfer-Encoding: 7bit
"Position of Item"
"Test1 Position"
"Test2 Position"
"Test 3 Position"
"Test 4 Position"
"Extra"
...
--Apple-Mail=_785C0CB9-CB2F-43BE-AD5F-D7DEF5F97EFC--
While I am surprised by this behavior (I would have expected to only see the attachment that the user taps on) this is not hard to solve.
You can easily write a multipart MIME scanner that splits up a multipart message into the individual attachments.
Are you certain that you actually have individual files inside the email that is being sent? Inspect the raw message.
Also you might have set up your UTI registration incorrectly, please read my writeup: http://www.cocoanetics.com/2012/09/fun-with-uti/
Usually if you open an app via the File Open In (fom another app) you are getting passed a file URL that points to a copy of said file places in the receiving app's Document/Inbox folder. Inspect this URL to see what kind of file the system places there. If you don't remove it, then you can copy the contents of your app folder to the desktop with Xcode organizer.