ZBurm
ZBurm

Reputation: 121

NSData on firebase

I am trying to get my app connected with firebase but one of the things I need is to be able to save a PDF file to firebase or an NSData object. So using objective c how can I save a NSData object as a value in firebase?

Upvotes: 2

Views: 1224

Answers (1)

rob mayoff
rob mayoff

Reputation: 385590

The simplest solution is to encode your NSData as an NSString using base 64 encoding:

NSString *stringForm = [data base64EncodedStringWithOptions:0];

and then convert it back later:

NSData *data = [[NSData alloc] initWithBase64EncodedString:stringForm options:0];

iOS 7.0 and OS X 10.9 support these methods. If you need to support an earlier OS, you can find several base-64 encoding implementations on the web.

UPDATE

In Swift:

let stringForm = data.base64EncodedStringWithOptions([])

let dataOption = NSData(base64EncodedString: stringForm, options: [])

Note that dataOption is an Optional<NSData>, and is nil if stringForm doesn't contain valid base-64-encoded data.

Upvotes: 6

Related Questions