Как поделиться файлами с помощью листа общего доступа в iOS?

Я хочу поделиться некоторыми файлами, которые у меня есть локально в моем приложении, с помощью функции «Общий лист» на iPhone. Я показываю файл в UIWebView, и когда пользователь щелкает лист обмена, я хочу показать параметры (электронная почта, WhatsApp и т. д.), чтобы поделиться файлом, отображаемым на UIWebView. Я знаю, что мы можем использовать

func displayShareSheet(shareContent:String) {                                                                           
    let activityViewController = UIActivityViewController(activityItems: [shareContent as NSString], applicationActivities: nil)
    presentViewController(activityViewController, animated: true, completion: {})    
}

например, чтобы поделиться строкой. Как изменить этот код, чтобы обмениваться документами?


person user3528213    schedule 07.03.2016    source источник


Ответы (3)


Swift 4.2 и Swift 5

Если у вас уже есть файл в каталоге и вы хотите поделиться им, просто добавьте его URL-адрес в activityItems:

let fileURL = NSURL(fileURLWithPath: "The path where the file you want to share is located")

// Create the Array which includes the files you want to share
var filesToShare = [Any]()

// Add the path of the file to the Array
filesToShare.append(fileURL)

// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

// Show the share-view
self.present(activityViewController, animated: true, completion: nil)

Если вам нужно создать файл:

Я использую это расширение для создания файлов из Data (прочитайте комментарии в коде, чтобы понять, как это работает):

Как и в ответе typedef, получите текущий каталог документов:

/// Get the current directory
///
/// - Returns: the Current directory in NSURL
func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory as NSString
}

Расширение для Data:

extension Data {

    /// Data into file
    ///
    /// - Parameters:
    ///   - fileName: the Name of the file you want to write
    /// - Returns: Returns the URL where the new file is located in NSURL
    func dataToFile(fileName: String) -> NSURL? {

        // Make a constant from the data
        let data = self

        // Make the file path (with the filename) where the file will be loacated after it is created
        let filePath = getDocumentsDirectory().appendingPathComponent(fileName)

        do {
            // Write the file from data into the filepath (if there will be an error, the code jumps to the catch block below)
            try data.write(to: URL(fileURLWithPath: filePath))

            // Returns the URL where the new file is located in NSURL
            return NSURL(fileURLWithPath: filePath)

        } catch {
            // Prints the localized description of the error from the do block
            print("Error writing the file: \(error.localizedDescription)")
        }

        // Returns nil if there was an error in the do-catch -block
        return nil

    }

}

Примеры использования:

Поделиться файлами изображений:

// Your image
let yourImage = UIImage()

в png-файле

// Convert the image into png image data
let pngImageData = yourImage.pngData()

// Write the png image into a filepath and return the filepath in NSURL
let pngImageURL = pngImageData?.dataToFile(fileName: "nameOfYourImageFile.png")

// Create the Array which includes the files you want to share
var filesToShare = [Any]()

// Add the path of png image to the Array
filesToShare.append(pngImageURL!)

// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

// Show the share-view
self.present(activityViewController, animated: true, completion: nil)

в jpg-файле

// Convert the image into jpeg image data. compressionQuality is the quality-compression ratio in % (from 0.0 (0%) to 1.0 (100%)); 1 is the best quality but have bigger filesize
let jpgImageData = yourImage.jpegData(compressionQuality: 1.0)

// Write the jpg image into a filepath and return the filepath in NSURL
let jpgImageURL = jpgImageData?.dataToFile(fileName: "nameOfYourImageFile.jpg")

// Create the Array which includes the files you want to share
var filesToShare = [Any]()

// Add the path of jpg image to the Array
filesToShare.append(jpgImageURL!)

// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

// Show the share-view
self.present(activityViewController, animated: true, completion: nil)

Поделиться текстовыми файлами:

// Your String including the text you want share in a file
let text = "yourText"

// Convert the String into Data
let textData = text.data(using: .utf8)

// Write the text into a filepath and return the filepath in NSURL
// Specify the file type you want the file be by changing the end of the filename (.txt, .json, .pdf...)
let textURL = textData?.dataToFile(fileName: "nameOfYourFile.txt")

// Create the Array which includes the files you want to share
var filesToShare = [Any]()

// Add the path of the text file to the Array
filesToShare.append(textURL!)

// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

// Show the share-view
self.present(activityViewController, animated: true, completion: nil)

Другие файлы:

Вы можете создать файл из всего, что находится в формате Data, и, насколько я знаю, почти все в Swift можно преобразовать в Data, например String, Int, Double, Any...:

// the Data you want to share as a file
let data = Data()

// Write the data into a filepath and return the filepath in NSURL
// Change the file-extension to specify the filetype (.txt, .json, .pdf, .png, .jpg, .tiff...)
let fileURL = data.dataToFile(fileName: "nameOfYourFile.extension")

// Create the Array which includes the files you want to share
var filesToShare = [Any]()

// Add the path of the file to the Array
filesToShare.append(fileURL!)

// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

// Show the share-view
self.present(activityViewController, animated: true, completion: nil)
person CaOs433    schedule 10.03.2019
comment
это отличный ответ для Swift 4 - отлично работает для всех типов файлов - person nc14; 10.06.2019
comment
Обязательно используйте кнопку ActivityViewController.popoverPresentationController?.sourceView = или представление в случае iPad. - person Raj Kiran; 28.07.2021

Я хочу поделиться своим решением UIActivityViewController и поделиться текстом в виде файла изображения. Это решение работает для обмена через почту и даже для сохранения в Dropbox.

@IBAction func shareCsv(sender: AnyObject) {
    //Your CSV text
    let str = self.descriptionText.text!
    filename = getDocumentsDirectory().stringByAppendingPathComponent("file.png")

    do {
        try str.writeToFile(filename!, atomically: true, encoding: NSUTF8StringEncoding)

        let fileURL = NSURL(fileURLWithPath: filename!)

        let objectsToShare = [fileURL]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        self.presentViewController(activityVC, animated: true, completion: nil)

    } catch {
        print("cannot write file")
        // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
    }

}

func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}
person typedef    schedule 07.03.2016

Вот версия Swift 3:

let dictToSave: [String: Any] = [
    "someKey": "someValue"
]

let jsonData = try JSONSerialization.data(withJSONObject: dictToSave, options: .prettyPrinted)

let filename = "\(self.getDocumentsDirectory())/filename.extension"
let fileURL = URL(fileURLWithPath: filename)
try jsonData.write(to: fileURL, options: .atomic)

let vc = UIActivityViewController(activityItems: [fileURL], applicationActivities: [])

self.present(vc, animated: true)


func getDocumentsDirectory() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}
person KBog    schedule 22.03.2018
comment
Работает как шарм. Спасибо, это очень хороший код, и его легче понять, чем официальную документацию. - person sznowicki; 30.12.2018