Giter Club home page Giter Club logo

base64's Introduction


WARNING: THIS PROJECT IS DEPRECATED

It will not receive any future updates or bug fixes. If you are using it, please migrate to another solution.


NOTE: As of iOS 7 and Mac OS 10.9, this library is not longer needed

In the iOS 7 and Mac OS 10.9 SDKs, Apple introduced new base64 methods on NSData that make it unnecessary to use a 3rd party base 64 decoding library. What's more, they exposed access to private base64 methods that are retrospectively available back as far as IOS 4 and Mac OS 6.

Although use of this library is no longer required, you may still find it useful, as it abstracts the complexity of supporting the deprecated Base64 methods for older OS versions, and also provides some additional utility functions, such as arbitrary wrap widths and NSString encoding.

Purpose

Base64 is a set of categories that provide methods to encode and decode data as a base-64-encoded string.

Supported OS & SDK Versions

  • Supported build target - iOS 7.0 / Mac OS 10.9 (Xcode 5.0, Apple LLVM compiler 5.0)
  • Earliest supported deployment target - iOS 5.0 / Mac OS 10.7
  • Earliest compatible deployment target - iOS 4.3 / Mac OS 10.6

NOTE: 'Supported' means that the library has been tested with this version. 'Compatible' means that the library should work on this iOS version (i.e. it doesn't rely on any unavailable SDK features) but is no longer being tested for compatibility and may require tweaking or bug fixes to run correctly.

ARC Compatibility

As of version 1.1, Base64 requires ARC. If you wish to use Base64 in a non-ARC project, just add the -fobjc-arc compiler flag to the Base64.m file. To do this, go to the Build Phases tab in your target settings, open the Compile Sources group, double-click Base64.m in the list and type -fobjc-arc into the popover.

If you wish to convert your whole project to ARC, comment out the #error line in Base64.m, then run the Edit > Refactor > Convert to Objective-C ARC... tool in Xcode and make sure all files that you wish to use ARC for (including Base64.m) are checked.

Thread Safety

All the Base64 methods should be safe to call from multiple threads concurrently.

Installation

To use the Base64 category in an app, just drag the category files (demo files and assets are not needed) into your project and import the header file into any class where you wish to make use of the Base64 functionality.

NSData Extensions

Base64 extends NSData with the following methods:

+ (NSData *)dataWithBase64EncodedString:(NSString *)string;

Takes a base-64-encoded string and returns an autoreleased NSData object containing the decoded data. Any non-base-64 characters in the string are ignored, so it is safe to pass a string containing line breaks or other delimiters.

- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;

Encodes the data as a base-64-encoded string and returns it. The wrapWidth argument allows you to specify the number of characters at which the output should wrap onto a new line. The value of wrapWidth must be a multiple of four. Values that are not a multiple of four will be truncated to the nearest multiple. A value of zero indicates that the data should not wrap.

- (NSString *)base64EncodedString;

Encodes the data as a base-64-encoded string without any wrapping (line breaks).

NSString Extensions

Base64 extends NSString with the following methods:

+ (NSString *)stringWithBase64EncodedString:(NSString *)string;

Takes a base-64-encoded string and returns an autoreleased NSString object containing the decoded data, interpreted using UTF8 encoding. The vast majority of use cases for Base64 encoding use Ascii or UTF8 strings, so this should be sufficient for most purposes. If you do need to decode string data in an encoding other than UTF8, convert your string to an NSData object first and then use the NSData dataWithBase64EncodedString: method instead.

- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;

Converts the string data to UTF8 data and then encodes the data as a base-64-encoded string and returns it. The wrapWidth argument allows you to specify the number of characters at which the output should wrap onto a new line. The value of wrapWidth must be a multiple of four. Values that are not a multiple of four will be truncated to the nearest multiple. A value of zero indicates that the data should not wrap.

- (NSString *)base64EncodedString;

Encodes the string as UTF8 data and then encodes that as a base-64-encoded string without any wrapping (line breaks).

- (NSString *)base64DecodedString;

Treats the string as a base-64-encoded string and returns an autoreleased NSString object containing the decoded data, interpreted using UTF8 encoding. Any non-base-64 characters in the string are ignored, so it is safe to use a string containing line breaks or other delimiters.

- (NSData *)base64DecodedData;

Treats the string as base-64-encoded data and returns an autoreleased NSData object containing the decoded data. Any non-base-64 characters in the string are ignored, so it is safe to use a string containing line breaks or other delimiters.

base64's People

Contributors

billinghamj avatar nicklockwood avatar yonran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

base64's Issues

EXC

+ (NSString *)stringWithBase64EncodedString:(NSString *)string
{
    NSData *data = [NSData dataWithBase64EncodedString:string];
    if (data)
    {
        return [[self alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return nil;
}

screen shot 2014-02-07 at 3 36 01 pm

Unable to decode

I have an encoded string getting from one of the node element of xml(soap response).

This is the encoded string:

UEsDBBQAAAAIAFyEdUFb+9U/vgAAAIABAAAIAAAAZGF0YS54bWyNkM0KwjAQhO+C7xD6AM1WPXhII4J400sFj2W1ixTSpCSx1bc3xoo/p+Y0M2S/ZEesbo1iHVlXG50nWQoJcx51hcpoypM7uWQlpxOxp36DHgvywTEmDnhSFGUwDhU5S215NhXJDGYAy/mCjT6C/xL+sRobkkejL6yoqWdb85QjYHFugHnCRs4hg4XgUQ85WsLXqzvBP+b9hbs+l6Yliz4UJNcB/psM95palx2q63N7gBRA8E8UC+PvxgT/6vIBUEsDBBQAAAAIAFyEdUFujsgBOAAAADYAAAAVAAAAVk1fQV9NU1RfU0FMRVNSRVAueG1ss7GvyM1RKEstKs7Mz7NVMtQzUFIoLknMS0nMyc9LtVWqTC1Wsrfj5bLxSy13SSxJDE4tUdC3AwBQSwECFAAUAAAACABchHVBW/vVP74AAACAAQAACAAAAAAAAAAAAAAAAAAAAAAAZGF0YS54bWxQSwECFAAUAAAACABchHVBbo7IATgAAAA2AAAAFQAAAAAAAAAAAAAAAADkAAAAVk1fQV9NU1RfU0FMRVNSRVAueG1sUEsFBgAAAAACAAIAeQAAAE8BAAAAAAA=

And I tried to use the following method to decode:

NSData *outputData = [NSData dataWithBase64EncodedString:@"UEsDBBQAAAAIAFyEdUFb+9U/vgAAAIABAAAIAAAAZGF0YS54bWyNkM0KwjAQhO+C7xD6AM1WPXhII4J400sFj2W1ixTSpCSx1bc3xoo/p+Y0M2S/ZEesbo1iHVlXG50nWQoJcx51hcpoypM7uWQlpxOxp36DHgvywTEmDnhSFGUwDhU5S215NhXJDGYAy/mCjT6C/xL+sRobkkejL6yoqWdb85QjYHFugHnCRs4hg4XgUQ85WsLXqzvBP+b9hbs+l6Yliz4UJNcB/psM95palx2q63N7gBRA8E8UC+PvxgT/6vIBUEsDBBQAAAAIAFyEdUFujsgBOAAAADYAAAAVAAAAVk1fQV9NU1RfU0FMRVNSRVAueG1ss7GvyM1RKEstKs7Mz7NVMtQzUFIoLknMS0nMyc9LtVWqTC1Wsrfj5bLxSy13SSxJDE4tUdC3AwBQSwECFAAUAAAACABchHVBW/vVP74AAACAAQAACAAAAAAAAAAAAAAAAAAAAAAAZGF0YS54bWxQSwECFAAUAAAACABchHVBbo7IATgAAAA2AAAAFQAAAAAAAAAAAAAAAADkAAAAVk1fQV9NU1RfU0FMRVNSRVAueG1sUEsFBgAAAAACAAIAeQAAAE8BAAAAAAA="];
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
//NSAssert([outputString isEqualToString:inputString], @"OutputEqualsInput test failed");
NSLog(@"outputString: %@",outputString);

But I can't get the decoded string... Any help will be much appreciated. Thanks.

Error

Hello.
I have added this to my project. I cannot compile my iOS application because it is throwing this error:
MyProject/NSStrinAdditions.h:44:9: Assigning to 'const unsigned char *' from incompatible type 'const void *'

How can I solve this?
Thank you.

Padding

Does the library properly account for padding?

Unrecognised selector crash

I'm trying to use this library in a fairly simple way...

I have already imported the header into the top of this file

#import "Base64.h"

I am trying to use the NSString extension method like this...

NSString * encodedString = [string base64EncodedString];

And when the line of code runs, i get the following error...


-[__NSCFConstantString base64EncodedString]: unrecognized selector sent to instance 0x93e1c

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString base64EncodedString]: unrecognized selector sent to instance 0x93e1c'
*** First throw call stack:
(0x3354c3e7 0x3b3dc963 0x3354ff31 0x3354e64d 0x334a6208 0x5bd49 0x5b717 0x5ad6b 0x35446087 0x3544603b 0x35446015 0x354458cb 0x35445db9 0x3536e5f9 0x3535b8e1 0x3535b1ef 0x3704e5f7 0x3704e227 0x335213e7 0x3352138b 0x3352020f 0x3349323d 0x334930c9 0x3704d33b 0x353af2b9 0x59491 0x59418)
libc++abi.dylib: terminate called throwing an exception

Whats up?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.