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) 
相關問題