0

I have this NSURLSessionDataTask and I'm getting a Base64 string from the completionHandler. I'm having trouble converting it back into plaintext using the NS Encoding libraries even after looking up various posts.

Decode a Base64 string in objective-c , Base64 Decoding in iOS 7+ , Convert between UIImage and Base64 string

String from completionHandler^(Data) = InsnYWN0aW9uJzondGVzdCcsICdpbnB1dCc6J3Rlc3QnfSI=

Code used to try and decode:

NSString *baseString =[data base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];

I was hoping someone could lead me in the right direction. Thanks.

Full Function - inspired by (http://hayageek.com/ios-nsurlsession-example/):

-(void) httpPostWithCustomDelegate
{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSURL * url = [NSURL URLWithString:@"http://XXX.XXX.XXX.XXX"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSData * params =[@"{'action':'test', 'input':'test'}" dataUsingEncoding:NSUTF8StringEncoding];
NSString * newParams = [params base64EncodedStringWithOptions:0];

[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[newParams dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                       //NSLog(@"Response:%@ %@\n", response, error);
                                                       if(error == nil)
                                                       {
                                                           NSString *baseString =[data base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
                                                           //NSString *baseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                           NSLog(@"Data = %@",baseString);
                                                       }

                                                   }];

[dataTask resume];

}

Community
  • 1
  • 1
mant1s
  • 1
  • 2

1 Answers1

1

data contains the base64 encoded string (which represented data that represented some original object). So you need to convert data to the actual NSData representing whatever it was that was originally encoded.

Basically you need to reverse the original encoding steps.

If you are using iOS 7 or later you can do:

NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //NSLog(@"Response:%@ %@\n", response, error);
        if (data) {
            NSData *originalData = [[NSData alloc] initWithBase64EncodedData:data options:0];
            // Do something with the decoded data
        } else {
            NSLog(@"error = %@", error);
        }
}];
rmaddy
  • 314,917
  • 42
  • 532
  • 579