Deencoding Base64 en iOS 7+

He codificado texto ( NSString ) utilizando la nueva API NSData Class que se agrega en iOS7.

usando esto

 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding; 

aquí está mi código

 NSString *base64EncodedString = [[myText dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]; NSLog(@"%@", base64EncodedString); 

Estoy buscando decodificarlo

Swift 3+

 let plainString = "foo" 

Codificación

 let plainData = plainString.data(using: .utf8) let base64String = plainData?.base64EncodedString() print(base64String!) // Zm9v 

Desencoding

 if let decodedData = Data(base64Encoded: base64String!), let decodedString = String(data: decodedData, encoding: .utf8) { print(decodedString) // foo } 

Swift <3

 let plainString = "foo" 

Codificación

 let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding) let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) print(base64String!) // Zm9v 

Desencoding

 let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(rawValue: 0)) let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding) print(decodedString) // foo 

C objective

 NSString *plainString = @"foo"; 

Codificación

 NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding]; NSString *base64String = [plainData base64EncodedStringWithOptions:0]; NSLog(@"%@", base64String); // Zm9v 

Desencoding

 NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding]; NSLog(@"%@", decodedString); // foo 

En caso de que quiera escribir un código alternativo, la deencoding desde base64 ha estado presente en iOS desde el principio por advertencia de NSURL :

 NSURL *URL = [NSURL URLWithString: [NSString stringWithFormat:@"data:application/octet-stream;base64,%@", base64String]]; return [NSData dataWithContentsOfURL:URL];