Question

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:

  • First file user touches, file processed as expected. user can touch as many times and all is good.
  • Any subsequent file touched, the email's raw source, minus email header, will be saved as a file represented by the NSURL.
  • This behaviour has been observed on iphone 6.0.1 and ipad 5.1.1.

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--
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top