Saranjith
Saranjith

Reputation: 11567

Check pdf is Password protected or not before iOS 11.0

I'm getting a url which contains an pdf document.

I need to ask the user for the password if it is protected.

The same is able to do after iOS 11.0 using PDFkit.

    if #available(iOS 11.0, *) {
                    if let pdfDocument = PDFDocument(url: url) {

                        print(pdfDocument.isEncrypted) 
                        print(pdfDocument.isLocked) 

                        if pdfDocument.isEncrypted {
                            // Its password protected
                        }
                    }
    }
    else {

    // Earlier versions..
    }

Is there any way to do this? without using any third party if possible

Upvotes: 2

Views: 1418

Answers (1)

qtngo
qtngo

Reputation: 1679

For iOS 10 or earlier, you should use CGPDFDocument:

public func isLocked(fileURL: URL) -> Bool? {
    guard let document = CGPDFDocument(fileURL as CFURL) else { return nil }
    return !document.isUnlocked
}

Upvotes: 3

Related Questions