写作:工作时不允许操作pdf

时间:2016-09-14 02:02:58

标签: ios swift pdf swift2

我正在处理使用Core Text进行读写的PDF文件。 下面的代码是在PDF文件上绘制内容。

let fileName = "pdffilename.pdf"
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0] as NSString
    let pathForPDF = documentsDirectory.stringByAppendingString(fileName)
    UIGraphicsBeginPDFContextToFile(pathForPDF, CGRectZero, nil)
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0,612,792), nil)
    pagedataforPortaits()
    UIGraphicsEndPDFContext()

此代码在模拟器上运行得非常好但是当我使用真实设备时,我收到错误

<Error>: CGDataConsumerCreateWithFilename: failed to open `/var/mobile/Containers/Data/Application/CADA69EB-8879-45EE-BBE0-477AC810BB86/Documentspdffilename.pdf' for writing: Operation not permitted.
deflateEnd: error -3: (null).
deflateEnd: error -3: (null).
<Error>: CGPDFContextCreate: failed to create PDF context delegate.

任何建议??

1 个答案:

答案 0 :(得分:0)

路径字符串在文档目录和文件名之间需要一个"/"分隔符。

Swift 5.1:

let fileName = "pdffilename.pdf"
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let pathForPDF = documentsDirectory + "/" + fileName