0

I want to select a pdf from icloud and convert it into base64 so that i can upload it to further .I have selected the file from icloud but i ab not able to convert it into base64

   @IBAction func PdfBtn(_ sender: Any) {

    let importMenu = UIDocumentMenuViewController(documentTypes: ["public.composite-content"], in: .import)
    importMenu.delegate = self
    present(importMenu, animated: true, completion: nil)

}


@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {


    let link = url as URL
    print("The Url is : \(link)")






}

@available(iOS 8.0, *)
public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

    documentPicker.delegate = self
    present(documentPicker, animated: true, completion: nil)


}





func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    print("we cancelled")

    dismiss(animated: true, completion: nil)


}

please help .

  • ok but that is not the issue ..i want to covert the selected file to base 64 – Subhojit Mandal Jun 15 '17 at 03:23
  • And what did you try? base64 is 'binary to text' encoding scheme. First, you need dome instance of Data and next just use call data.base64EncodedData() or data.base64EncodedString(), which gives you the encoded data / String. – user3441734 Jun 15 '17 at 05:24

1 Answers1

0

ok, see some 'example'

let url = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Documents/hello.pdf")

let data = try! Data(contentsOf: url)

let base64Data = data.base64EncodedData()
let str0 = String(bytes: data[0..<9], encoding: .ascii) // "%PDF-1.3\n"
let str1 = String(bytes: base64Data[0..<12], encoding: .ascii) // "JVBERi0xLjMK"

let base64String = data.base64EncodedString()
let str2 = String(base64String.characters.prefix(12)) // "JVBERi0xLjMK"

let data2 = Data(base64Encoded: str2)
let str3 = String(bytes: data2!, encoding: .ascii) // "%PDF-1.3\n"

Please, see that I hard coded the number of bytes and number for characters to make some usable results (Each base64 digit represents exactly 6 bits of data). Don't use this in your code ...

user3441734
  • 16,722
  • 2
  • 40
  • 59