1

I've been searching and haven't found many questions nor answers about sending audio files via iMessage Applications. I have been trying and am just not sure how to go about it. Here is a snippet of my code:

@IBAction func pressSend(_ sender: Any) {
        let filePath = Bundle.main.path(forResource: "yaddadi", ofType: "m4a")
        let fileURL = NSURL(string: filePath!)

        if let conversation = activeConversation
        {
            conversation.insertAttachment(fileURL as! URL, withAlternateFilename: nil, completionHandler: nil)
        }
    }

When I run the app and click on the button that is attached to this function it inserts tiny little picture that appears to be nothing and I cannot even send that through the messages. Please help!

Austinn97
  • 25
  • 1
  • 4

1 Answers1

0

You're almost there. Perhaps you're just missing the URL scheme. Are you seeing this error in the console?

CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme

Try:

let path = Bundle.main.path(forResource: "yaddadi", ofType: "m4a")!
let url = URL(string: "file://\(path)")! // Note the addition of "file://" here.
activeConversation?.insertAttachment(url, withAlternateFilename: nil, completionHandler: nil)
JWK
  • 3,345
  • 2
  • 19
  • 27
  • When I tried that I got an error that says "fatal error: unexpectedly found nil while unwrapping an Optional value 2017-01-16 10:29:08.647001 MessagesExtension[7847:3394193] fatal error: unexpectedly found nil while unwrapping an Optional value" – Austinn97 Jan 16 '17 at 16:31
  • Set a breakpoint to determine where the `nil` value is. I'm force unwrapping in my example because I'm certain the `path` and `url` won't be `nil`—you may not necessarily want to do that. Since you're also force unwrapping `filePath` in your example, I'm assuming the `nil` value is `fileURL`. – JWK Jan 16 '17 at 20:00
  • It is pointing at the fileURL declaration saying EXC_BAD_INSTRUCTION but it wasn't doing that before I added "file://\(filePath)")! and I know the filePath is correct because I have the same path being used in an AVAudioPlayer that is working correctly. – Austinn97 Jan 16 '17 at 22:59
  • You may not be sharing the file with your iMessage App extension—make sure the target membership for the file has been updated, so the necessary targets can access it. – JWK Jan 17 '17 at 02:57
  • Messages extension is checked under target membership. – Austinn97 Jan 17 '17 at 17:36
  • I don't know the specifics of your project, but I wonder if something else is at play. Try this: 1) make a new iMessage Application project from the template 2) override `touchesBegan(_:with:)` in `MessagesViewController` and add the lines from my answer 3) add your audio file to the project with the correct target membership. Then, run the project and tap the view for your app's `MessagesViewController` (in Messages). I've tried this and it does work without issue / answer your question as stated :) – JWK Jan 18 '17 at 03:24
  • What do you mean by override touchesBegan? I don't see a function named that in my MessagesViewController? Sorry I'm new to all of this. – Austinn97 Jan 18 '17 at 17:55
  • In `MessagesViewController`, start typing "touchesBegan" and then press return to complete the signature. Put your code inside this method. – JWK Jan 19 '17 at 06:40
  • So would I then have to call touchesBegan in another function to connect it since it is not an IBAction? – Austinn97 Jan 20 '17 at 20:53
  • No. Your `MessagesViewController` is a `UIViewController` subclass, which inherits from `UIResponder`. Take a look at the docs for a deeper understanding of what that means. All you need to do is put the above code inside `override func touchesBegan(_ touches: Set, with event: UIEvent?) { /* HERE */ }`, run your app and then tap the view (which probably still has a label that says "Hello World"). – JWK Jan 23 '17 at 06:15