Our first program will demonstrate base64 encoding and decoding. Here’s the full source code.
To run the program, save the code in a file with a .m extension (e.g., base64_encoding.m) and use the following commands:
The string encodes to slightly different values with the standard and URL-compatible base64 encoders (trailing + vs -) but they both decode to the original string as desired.
Note that Objective-C doesn’t have a built-in URL-compatible base64 encoder, so we manually replace the characters to achieve the same result. In practice, you might want to create a category on NSData or NSString to encapsulate this functionality for reuse.