Aweda
Aweda

Reputation: 473

How do I open .msg and .eml files in swift?

I have made some attempts to open .msg and .eml files in swift 3 but all to no avail. I have used

UIApplication.shared.open(NSURL(string: documentURL) as! URL, options: [:], completionHandler: nil);

and

let url: URL! = NSURL.fileURL(withPath: documentURL);
            documentInterection = UIDocumentInteractionController(url: url);
            documentInterection.name = url.lastPathComponent;
            documentInterection.delegate = self;
            documentInterection.presentOptionsMenu(from: backButton, animated: true);

None of the above opened the file like any other (.png, .doc, etc) would - with the help of let webView = UIWebView(frame: self.view.frame); ...

The former presents me with a UIWebView, which wouldn't open it. The latter pops up options but opens a blank new mail message if Outlook or iOS Mail client is selected.

I need some help on how to open .msg and .eml files in swift 3?

Pls note: the documentURL is a url of the .msg or .eml file to be opened, coming from an API.

Links I tried: Open .msg and .eml files from iOS UIWebview

Upvotes: 2

Views: 1738

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70976

The problem you're having is

  • You can't open a document and display the content unless either your app or some other app on the phone can parse the document.
  • Neither your app nor any other app on your iPhone is capable of that, so
  • Opening either fails or works in a way you don't want.

You can't make Mail or Outlook display the content. If they open it, they'll do whatever they think should be done with it. If they want to make it an attachment to a new message, that's their choice.

For many formats-- the PNGs, etc, that you mention-- there are lots of apps that will open and display them. For these specific formats, there aren't, at least on iOS. As a result you're stuck-- unless you implement support for these documents in your own app, or convince some other app developer to do so, you won't be able to display them.

Upvotes: 1

Related Questions