Last active
January 1, 2017 21:20
-
-
Save benbahrenburg/8bdd9aaa087402a0a4fd5d65a04c6c26 to your computer and use it in GitHub Desktop.
Unlock a PDF document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let pdf = unlock(data: pdfData, password: "Hello World") { | |
print("You now have an unlocked CGPDFDocument") | |
} | |
func unlock(data: Data, password: String) -> CGPDFDocument? { | |
if let pdf = CGPDFDocument(CGDataProvider(data: data as CFData)!) { | |
guard pdf.isEncrypted == true else { return pdf } | |
guard pdf.unlockWithPassword("") == false else { return pdf } | |
if let cPasswordString = password.cString(using: String.Encoding.utf8) { | |
if pdf.unlockWithPassword(cPasswordString) { | |
return pdf | |
} | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment