How do I share an audio file which exists in my apps document directory to other apps?
To elaborate on this question, what I m
My answer is using for doing this with UIDocumentInteractionController.
I begin by instantiating a UIDocumentInteractionController at the top of my class
var controller = UIDocumentInteractionController()
Then I link up an IBAction to a share button on my nib or Storyboard:
@IBAction func SHARE(_ sender: Any) {
let dirPath: String = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask,
true)[0]
let recordingName = UserDefaults.standard.string(forKey: "recordingName")
let pathArray: [String] = [dirPath, recordingName!]
let filePathString: String = pathArray.joined(separator: "/")
controller = UIDocumentInteractionController(url: NSURL(fileURLWithPath: filePathString) as URL)
controller.presentOpenInMenu(from: CGRect.zero,
in: self.view,
animated: true)
}
Swift 3.x:
let activityItem = URL.init(fileURLWithPath: Bundle.main.path(forResource: "fileName", ofType: "mp3")!)
let activityVC = UIActivityViewController(activityItems: [activityItem],applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
self.present(activityVC, animated: true, completion: nil)