NSData won't accept valid base64 encoded string
Although Martin's answer is correct, here is a quick and correct(!) way to fix the problem:
NSString *base64String = @"<the token>";NSUInteger paddedLength = base64String.length + (4 - (base64String.length % 4));NSString* correctBase64String = [base64String stringByPaddingToLength:paddedLength withString:@"=" startingAtIndex:0];
Here is a solution that pads the Base-64 string appropriately and works in iOS 4+:
NSData+Base64.h
@interface NSData (Base64)/** Returns a data object initialized with the given Base-64 encoded string. @param base64String A Base-64 encoded NSString @returns A data object built by Base-64 decoding the provided string. Returns nil if the data object could not be decoded. */- (instancetype) initWithBase64EncodedString:(NSString *)base64String;/** Create a Base-64 encoded NSString from the receiver's contents @returns A Base-64 encoded NSString */- (NSString *) base64EncodedString;@end
NSData+Base64.m
@interface NSString (Base64)- (NSString *) stringPaddedForBase64;@end@implementation NSString (Base64)- (NSString *) stringPaddedForBase64 { NSUInteger paddedLength = self.length + (self.length % 3); return [self stringByPaddingToLength:paddedLength withString:@"=" startingAtIndex:0];}@end@implementation NSData (Base64)- (instancetype) initWithBase64EncodedString:(NSString *)base64String { return [self initWithBase64Encoding:[base64String stringPaddedForBase64]];}- (NSString *) base64EncodedString { return [self base64Encoding];}@end