How do I share an Audio File in an App using Swift 3?

后端 未结 2 1107
野趣味
野趣味 2021-01-14 16:27

Sharing an Audio File in Swift

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

相关标签:
2条回答
  • 2021-01-14 17:18

    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)     
    }
    
    0 讨论(0)
  • 2021-01-14 17:31

    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)
    
    0 讨论(0)
提交回复
热议问题