首页 > 解决方案 > 如何将 b64 保存为 pdf?

问题描述

我从 API 得到一个字节并转换为 b64,我想将 pdf 保存在 iPhone 中,但是当我调用我的函数时,我只得到这个:file:///Applications/Demos/yourFileName.pdf

我需要将来自 B64 字符串的 iPhone 下载文件夹中的文件另存为 pdf

我的代码中缺少什么?

func saveBase64StringToPDF(_ base64String: String) {

    guard
        var documentsURL = (FileManager.default.urls(for: .allApplicationsDirectory, in:.allDomainsMask)).last,
        let convertedData = Data(base64Encoded: base64String)
        else {
            debugPrint("Error")
        return
    }

    documentsURL.appendPathComponent("yourFileName.pdf")

    do {
        try convertedData.write(to: documentsURL, options: .atomic)
        pdfURL = documentsURL
        debugPrint("Aquí se guarda el pdf??")

    } catch {
        debugPrint("Error")
    }


    debugPrint(documentsURL)
}

标签: iosswiftxcode

解决方案


推荐阅读