简体   繁体   中英

Swift 4 sharing audio file (UIDocumentInteractionController)

I have this block of code from a previous project, my plan is to send an audio file(mp3) by UIDocumentInteractionController , but I specifically need that show also the possibility to share by WhatsApp. Also, I've added a UIAlertView which since ios9 has been deprecated. As you probably realized, this code is kind of "old". So I would appreciate it if you could suggest any option to make it work as at the moment since, in swift 4, it doesn't.

var documentationInteractionController: UIDocumentInteractionController? 
    @IBAction func ShareButton(_ sender: Any) {
        do {

            if let aString = URL(string: "whatsapp://app") {
                if UIApplication.shared.canOpenURL(aString) {

                    var savePath = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.waa").absoluteString

                    savePath = Bundle.main.path(forResource: "FILENAME", ofType: "mp3") ?? ""

                    documentationInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: savePath))
                    documentationInteractionController?.uti = "net.whatsapp.audio"
                    documentationInteractionController?.delegate = self as? UIDocumentInteractionControllerDelegate

                    documentationInteractionController?.presentOpenInMenu(from: CGRect(x: 0, y: 0, width: 0, height: 0), in: view, animated: true)
                } else {
                    _ = UIAlertView(title: "Error", message: "No WhatsApp installed on your iPhone", delegate: (self as! UIAlertViewDelegate), cancelButtonTitle: "OK", otherButtonTitles: "")
                }
            }
         }
      }

First you will need to add WhatsApp in your info.plist file in the LSApplicationQueriesSchemes array so your app will be allowed to query for the scheme whatsapp.

Then here is your code updated for Swift 4.2, the "do" is useless and I use UIAlertController.

@IBAction func share(_ sender: UIButton) {
    if let aString = URL(string: "whatsapp://app") {
        if UIApplication.shared.canOpenURL(aString) {

            var fileUrl = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.waa").absoluteString
            fileUrl = Bundle.main.path(forResource: "FILENAME", ofType: "mp3") ?? ""

            documentationInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: fileUrl))
            documentationInteractionController?.uti = "net.whatsapp.audio"
            documentationInteractionController?.delegate = self

            documentationInteractionController?.presentOpenInMenu(from: CGRect(x: 0, y: 0, width: 0, height: 0), in: view, animated: true)
        } else {
            let alert = UIAlertController(title: "Error", message: "No WhatsApp installed on your iPhone.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
                NSLog("The \"OK\" alert occured.")
            }))
            self.present(alert, animated: true, completion: nil)
        }
    }
}

Don't forget to adopt the UIDocumentInteractionControllerDelegate protocol. I didn't touch the code for your audio file because I know nothing about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM