-1

I have multiple mp3 files in my project. I would like to know how I can access this file (file path) and attach it to the text message I will be sending out.

Does anybody know how to do this? Please provide some code if possible!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JohnCarp
  • 49
  • 8

1 Answers1

1

MFMessageComposeViewController has a few handy methods for you:

func addAttachmentURL(_ attachmentURL: NSURL, withAlternateFilename alternateFilename: String?) -> Bool

and

func addAttachmentData(_ attachmentData: NSData,typeIdentifier uti: String, filename filename: String) -> Bool

You'd probably want to do something like:

let pFileUrl = NSURL(fileURLWithPath:pSongPath];
do {
   let pData = try NSData(contentsOfURL: pFileUrl, options: NSDataReadingOptions())
   pMailComposer.addAttachmentData(pData, mimeType:"audio/mpeg" fileName:@"song.mp3")
} catch let error as NSError {
   print("error while trying to load data - \(error.localizedDescription)")
}

(I didn't run this through a compiler so I might be off an optional or two)

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • How would I add my mp3 file to this code? Sorry I am kinda new at this! Thanks! – JohnCarp Jan 09 '16 at 03:33
  • I tried doing this but it said my file could not be found? pSongPath would be where i put my file name right? So if i had an mp3 called -- iloveStack.mp3 it would go there? – JohnCarp Jan 09 '16 at 04:03
  • the file URL would be the path to your file. Where do you keep your file? Within your app bundle or somewhere else? – Michael Dautermann Jan 09 '16 at 11:59