1

如何在用swift编写的iOS应用程序中共享音频文件。 (通过分享我的意思是,当用户记录有音频后击中分享按钮,他们可以通过电子邮件发送音频文件给别人,或者把它放入的SoundCloud应用程序或类似)。如何在使用Swift 3的应用程序中共享音频文件?

我可以使用下面来实现这一?

  • UIActivityViewController
  • UIDocumentInteractionController

我的应用程序,使它们应该能够分享的人的录音,但我已经搜查,搜查,并没有能够找到的代码示例如何做到这一点(但我知道这是可能的)。

回答

0

首先,在视图控制器类的顶部,其中共享是采取地方,我们把一个声明为UIDocumentInteractionController:

//在你的类的顶部你把

var controller = UIDocumentInteractionController() 

//然后一个IBAction为连接起来,分享按钮,在你的底栏是这样的:

@IBAction func SHARE(_ sender: Any) { 
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String 
    // Here we've used user defaults to store the name of an audio recording, but you could use a variable 
    let recordingName = UserDefaults.standard.value(forKey: "recordingName") as! String 
    let pathArray = [dirPath, recordingName] as [String] 
    let filePathString: String = pathArray.joined(separator: "/") as String 
    var path = Bundle.main.path(forResource: filePathString as! String?, ofType: "wav") 

    controller = UIDocumentInteractionController(url: NSURL(fileURLWithPath: filePathString as! String) as URL) 

    let button = sender as! Any 
    let view = self.view 
    controller.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)  
} 
3

斯威夫特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) 
相关问题