仅在11.3上生成pdf:swift的写入权限

时间:2018-04-17 06:06:01

标签: ios swift pdf

我在iOS 11.3上生成pdf时遇到错误。它适用于iOS 11.2。

CGDataConsumerCreateWithFilename: failed to open `/note.pdf' for writing: Operation not permitted.
deflateEnd: error -3: (null).

Pdf生成良好,但当我尝试访问它以进行上传时,会产生错误。代码相同:

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

let fileURL:URL = documentsURL.appendingPathComponent("note.pdf")

do {
   try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
   NSLog("Unable to create directory \(error.debugDescription)")
}

UIGraphicsBeginPDFContextToFile((fileURL.path as NSString).lastPathComponent as String?, CGRectZero, nil);

最后一行产生错误。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

您要将文件保存在根目录中,不能这样做,请尝试此操作

 let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

            let fileURL:URL = documentsURL.appendingPathComponent("note.pdf")

            do {
                try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
            } catch let error as NSError {
                NSLog("Unable to create directory \(error.debugDescription)")
            }

            UIGraphicsBeginPDFContextToFile(fileURL.appendingPathComponent("note.pdf").path, CGRect.zero, nil);
  

哪个会得到,根据你在做什么,你将创建一个   文件夹note.pdf然后将文件note.pdf保存在其中