How to convert Base64 and NSData data to string

I am new to IOS and I am doing a project where I am getting Base64 data from a web service. how to convert Base64 data to string and how to open pdf view in swift and also check if there is an app for PDF app or not in iPhone. And I want to know how to convert a string swift.Pomosch NSDATA in my example, this data is Base64 JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4 + CnN0cmVhbQp4nLVcza8ct5GH1rK9ejIU24lkO5GtJ8vWm5E87eY3uXtbYLHAYi8JdItySrABAjhA8v8fUuwmu35kF2fmxbsWDMxjk8VisapYX + TfbudJ6ds5 / 6c // VJ

I like this nsdata <25504446 2d312e34 0a25c7ec 8fa20a35 2030206f 626a0a3c 3c2f4c65 6e677468 20362030 20522f46 696c7465 72202f46 6c617465 4465636f 64653e3e 0ad617465 4465636f 64653e3e 0ad617465 4465636f 64653e3e 0ad617465 4465636f 64653e3e 0ad617465 4465636f 64653e3e 0ad637b769 c656b3bad9d6b3b2

+3


source to share


3 answers


From Base64

to Data

to String

.

let base64String = "dGhpcyBpcyBmb3IgdGVzdGluZw=="
if let data = Data(base64Encoded: base64String) {
    if let string = String(data: data, encoding: .utf8) {
        print(string)
    }
}

      



From Base64

to NSData

to String

.

let data = NSData(base64Encoded: base64String, options: .ignoreUnknownCharacters)
    var string = String(data: data, encoding: .utf8)
}

      

+2


source


Swift 3.0

Convenient string expansion. Hope will help you.



extension String {

    func fromBase64() -> String? {
        guard let data = Data(base64Encoded: self) else {
            return nil
        }

        return String(data: data, encoding: .utf8)
    }

    func toBase64() -> String {
        return Data(self.utf8).base64EncodedString()
    }
}

      

0


source


There are two parts to this question. First, convert the base 64 string to Data

/ NSData

. But you've already done that, so you don't need any help.

Second, converting this Data

/ NSData

to a string. But if you take a close look at this file, you will see that the data is a PDF file, not a text string. For example, if I save this as a file and look at it in a hex editor, I can clearly see it in PDF:

hex dump

You cannot simply convert this binary PDF data to a string. (Actually, why it was base64 encoded in the first place is because it was complex binary data.)

But you can, for example, use UIDocumentInteractionController

to preview the PDF file saved in the file.


For example:

// convert base 64 string to data

let base64 = "JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nLVcza8ct5GH1rK9ejIU24lkO5GtJ8vWm5E87eY3uXtbYLHAYi8JdItySrABAjhA8v8fUuwmu35kF2fmxbsWDMxjk8VisapYX+TfbudJ6ds5/6s/"
guard let data = Data(base64Encoded: base64) else {
    print("unable to convert base 64 string to data")
    return
}

// given the data was PDF, let save it as such

let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    .appendingPathComponent("test.pdf")
try! data.write(to: fileURL)

// if it was a string, you could convert the data to string, but this will fail
// because the data is a PDF, not a text string
//
// guard let string = String(data: data, encoding: .utf8) else {
//     print("Unable to convert data into string")
//     return
// }
// print(string)

// So, instead, let use `UIDocumentInteractionController` to preview the PDF:

let controller = UIDocumentInteractionController(url: fileURL)
controller.delegate = self
controller.presentPreview(animated: true)

      

Where the view controller matches UIDocumentInteractionControllerDelegate

:

extension ViewController: UIDocumentInteractionControllerDelegate {

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self

        // or, if this view controller is already in navigation controller, don't 
        // return `self`, like above, but instead return the navigation controller itself
        //
        // return navigationController!
    }

}

      

0


source







All Articles