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

后端 未结 2 1123
野趣味
野趣味 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)     
    }
    

提交回复
热议问题