我试图使用UIActivityViewController分享SVG文件,但只有消息和邮件可用选项。 如果我选择邮件,则没有任何附件。 有什么办法可以解决这个问题吗?
我试图使用UIActivityViewController分享SVG文件,但只有消息和邮件可用选项。 如果我选择邮件,则没有任何附件。 有什么办法可以解决这个问题吗?
你应该将字符串保存为数据到应用程序的临时或文档文件夹,然后使用URL作为活动项目,操作系统可以很好地处理它:
extension Data {
/// Data into file
///
/// - Parameters:
/// - fileName: the Name of the file you want to write, remember to include extension.
/// - Returns: Returns the URL where the new file is located in
func toFile(fileName: String) -> URL? {
var filePath: URL = URL(fileURLWithPath: NSTemporaryDirectory())
filePath.appendPathComponent(fileName)
do {
try write(to: filePath)
return filePath
} catch {
print("Error writing the file: \(error.localizedDescription)")
}
return nil
}
}
然后你可以这样做:
let textData = Data(yourSVGString.utf8)
let uniqueRandomName = String(UUID().uuidString.suffix(4))
let svgURL = textData.toFile(fileName: "\(uniqueRandomName).svg")
let ac = UIActivityViewController(activityItems: [svgURL], applicationActivities: nil)
self.present(ac, animated: true, completion: nil)