使用UIActivityViewController指定文件类型

时间:2017-07-17 23:39:19

标签: ios uiactivityviewcontroller airdrop

关注this问题后,如何使用UIActivityViewController指定我通过空投分享的文件的文件类型?

1 个答案:

答案 0 :(得分:0)

解决了这个问题:

let myFileManager = FileManager.default
    var activityArray = [NSURL]()
    activityArray.removeAll()

    for (i, data) in dataToShare.enumerated() {
        let docsurl = try! myFileManager.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
        let myurl = docsurl.appendingPathComponent("shareData\(i).gymProf")
        var myUrlString = myurl.absoluteString

        myUrlString = myUrlString.replacingOccurrences(of: "file://", with: "")
        myFileManager.createFile(atPath: myUrlString, contents: data, attributes: nil)
        activityArray.append(NSURL(fileURLWithPath: myUrlString))

        if myFileManager.fileExists(atPath: myUrlString) {
            print("File Exists at \(myUrlString)")
        } else {
            print("File not found")
        }

    }

    let activityViewController = UIActivityViewController(activityItems: activityArray, applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view

    self.present(activityViewController, animated: true, completion: nil)