'How can I encode a string to Base64 in Swift?
I want to convert a string to Base64. I found answers in several places, but it does not work anymore in Swift. I am using Xcode 6.2. I believe the answer might be work in previous Xcode versions and not Xcode 6.2.
Could someone please guide me to do this in Xcode 6.2?
The answer I found was this, but it does not work in my version of Xcode:
var str = "iOS Developer Tips encoded in Base64"
println("Original: \(str)")
// UTF 8 str from original
// NSData! type returned (optional)
let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)
// Base64 encode UTF 8 string
// fromRaw(0) is equivalent to objc 'base64EncodedStringWithOptions:0'
// Notice the unwrapping given the NSData! optional
// NSString! returned (optional)
let base64Encoded = utf8str.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
println("Encoded: \(base64Encoded)")
// Base64 Decode (go back the other way)
// Notice the unwrapping given the NSString! optional
// NSData returned
let data = NSData(base64EncodedString: base64Encoded, options: NSDataBase64DecodingOptions.fromRaw(0)!)
// Convert back to a string
let base64Decoded = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Decoded: \(base64Decoded)")
ref: http://iosdevelopertips.com/swift-code/base64-encode-decode-swift.html
Solution 1:[1]
I don’t have 6.2 installed but I don’t think 6.3 is any different in this regard:
dataUsingEncoding
returns an optional, so you need to unwrap that.
NSDataBase64EncodingOptions.fromRaw
has been replaced with NSDataBase64EncodingOptions(rawValue:)
. Slightly surprisingly, this is not a failable initializer so you don’t need to unwrap it.
But since NSData(base64EncodedString:)
is a failable initializer, you need to unwrap that.
Btw, all these changes were suggested by Xcode migrator (click the error message in the gutter and it has a “fix-it” suggestion).
Final code, rewritten to avoid force-unwraps, looks like this:
import Foundation
let str = "iOS Developer Tips encoded in Base64"
println("Original: \(str)")
let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)
if let base64Encoded = utf8str?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
{
println("Encoded: \(base64Encoded)")
if let base64Decoded = NSData(base64EncodedString: base64Encoded, options: NSDataBase64DecodingOptions(rawValue: 0))
.map({ NSString(data: $0, encoding: NSUTF8StringEncoding) })
{
// Convert back to a string
println("Decoded: \(base64Decoded)")
}
}
(if using Swift 1.2 you could use multiple if-lets instead of the map)
Swift 5 Update:
import Foundation
let str = "iOS Developer Tips encoded in Base64"
print("Original: \(str)")
let utf8str = str.data(using: .utf8)
if let base64Encoded = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) {
print("Encoded: \(base64Encoded)")
if let base64Decoded = Data(base64Encoded: base64Encoded, options: Data.Base64DecodingOptions(rawValue: 0))
.map({ String(data: $0, encoding: .utf8) }) {
// Convert back to a string
print("Decoded: \(base64Decoded ?? "")")
}
}
Solution 2:[2]
Swift
import Foundation
extension String {
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else {
return nil
}
return String(data: data, encoding: .utf8)
}
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
}
Solution 3:[3]
Swift 4.2
"abcd1234".data(using: .utf8)?.base64EncodedString()
Solution 4:[4]
Xcode 11.4 • Swift 5.2 or later
extension StringProtocol {
var data: Data { Data(utf8) }
var base64Encoded: Data { data.base64EncodedData() }
var base64Decoded: Data? { Data(base64Encoded: string) }
}
extension LosslessStringConvertible {
var string: String { .init(self) }
}
extension Sequence where Element == UInt8 {
var data: Data { .init(self) }
var base64Decoded: Data? { Data(base64Encoded: data) }
var string: String? { String(bytes: self, encoding: .utf8) }
}
let original = "iOS Developer Tips encoded in Base64"
let utf8Data = original.data // 36 bytes
let base64EncodedString = utf8Data.base64EncodedString() // aU9TIERldmVsb3BlciBUaXBzIGVuY29kZWQgaW4gQmFzZTY0\n"
let base64EncodedData = utf8Data.base64EncodedData() // 48 bytes"
print("base64EncodedData:", original.base64Encoded) // 48 bytes
print("base64EncodedString:", original.base64Encoded.string ?? "") // "aU9TIERldmVsb3BlciBUaXBzIGVuY29kZWQgaW4gQmFzZTY0"
print("base64DecodedData:", original.base64Encoded.string?.base64Decoded ?? "") // 36 bytes
print("base64DecodedString:", original.base64Encoded.base64Decoded?.string ?? "") // iOS Developer Tips encoded in Base64
Solution 5:[5]
You could just do a simple extension like:
import UIKit
// MARK: - Mixed string utils and helpers
extension String {
/**
Encode a String to Base64
:returns:
*/
func toBase64()->String{
let data = self.dataUsingEncoding(NSUTF8StringEncoding)
return data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
}
iOS 7 and up
Solution 6:[6]
Swift 4.0.3
import UIKit
extension String {
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self, options: Data.Base64DecodingOptions(rawValue: 0)) else {
return nil
}
return String(data: data as Data, encoding: String.Encoding.utf8)
}
func toBase64() -> String? {
guard let data = self.data(using: String.Encoding.utf8) else {
return nil
}
return data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0))
}
}
Solution 7:[7]
After thorough research I found the solution
Encoding
let plainData = (plainString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let base64String =plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
println(base64String) // bXkgcGxhbmkgdGV4dA==
Decoding
let decodedData = NSData(base64EncodedString: base64String, options:NSDataBase64DecodingOptions.fromRaw(0)!)
let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding)
println(decodedString) // my plain data
More on this http://creativecoefficient.net/swift/encoding-and-decoding-base64/
Solution 8:[8]
Swift 3 or 4
let base64Encoded = Data("original string".utf8).base64EncodedString()
Solution 9:[9]
Swift 5.1, Xcode 11:
public extension String {
/// Assuming the current string is base64 encoded, this property returns a String
/// initialized by converting the current string into Unicode characters, encoded to
/// utf8. If the current string is not base64 encoded, nil is returned instead.
var base64Decoded: String? {
guard let base64 = Data(base64Encoded: self) else { return nil }
let utf8 = String(data: base64, encoding: .utf8)
return utf8
}
/// Returns a base64 representation of the current string, or nil if the
/// operation fails.
var base64Encoded: String? {
let utf8 = self.data(using: .utf8)
let base64 = utf8?.base64EncodedString()
return base64
}
}
Solution 10:[10]
Swift 3 / 4 / 5.1
Here is a simple String
extension, allowing for preserving optionals in the event of an error when decoding.
extension String {
/// Encode a String to Base64
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
/// Decode a String from Base64. Returns nil if unsuccessful.
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else { return nil }
return String(data: data, encoding: .utf8)
}
}
Example:
let testString = "A test string."
let encoded = testString.toBase64() // "QSB0ZXN0IHN0cmluZy4="
guard let decoded = encoded.fromBase64() // "A test string."
else { return }
Solution 11:[11]
After all struggle I did like this.
func conversion(str:NSString)
{
if let decodedData = NSData(base64EncodedString: str as String, options:NSDataBase64DecodingOptions(rawValue: 0)),
let decodedString = NSString(data: decodedData, encoding: NSUTF8StringEncoding) {
print(decodedString)//Here we are getting decoded string
After I am calling another function for converting decoded string to dictionary
self .convertStringToDictionary(decodedString as String)
}
}//function close
//for string to dictionary
func convertStringToDictionary(text: String) -> [String:AnyObject]? {
if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
print(json)
if let stack = json!["cid"] //getting key value here
{
customerID = stack as! String
print(customerID)
}
} catch let error as NSError {
print(error)
}
}
return nil
}
Solution 12:[12]
FOR SWIFT 3.0
let str = "iOS Developer Tips encoded in Base64"
print("Original: \(str)")
let utf8str = str.data(using: String.Encoding.utf8)
if let base64Encoded = utf8str?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
{
print("Encoded: \(base64Encoded)")
if let base64Decoded = NSData(base64Encoded: base64Encoded, options: NSData.Base64DecodingOptions(rawValue: 0))
.map({ NSString(data: $0 as Data, encoding: String.Encoding.utf8.rawValue) })
{
// Convert back to a string
print("Decoded: \(base64Decoded)!")
}
}
Solution 13:[13]
Swift 4.2
var base64String = "my fancy string".data(using: .utf8, allowLossyConversion: false)?.base64EncodedString()
to decode, see (from https://gist.github.com/stinger/a8a0381a57b4ac530dd029458273f31a)
//: # Swift 3: Base64 encoding and decoding
import Foundation
extension String {
//: ### Base64 encoding a string
func base64Encoded() -> String? {
if let data = self.data(using: .utf8) {
return data.base64EncodedString()
}
return nil
}
//: ### Base64 decoding a string
func base64Decoded() -> String? {
if let data = Data(base64Encoded: self) {
return String(data: data, encoding: .utf8)
}
return nil
}
}
var str = "Hello, playground"
print("Original string: \"\(str)\"")
if let base64Str = str.base64Encoded() {
print("Base64 encoded string: \"\(base64Str)\"")
if let trs = base64Str.base64Decoded() {
print("Base64 decoded string: \"\(trs)\"")
print("Check if base64 decoded string equals the original string: \(str == trs)")
}
}
Solution 14:[14]
SwiftyBase64 (full disclosure: I wrote it) is a native Swift Base64 encoding (no decoding library. With it, you can encode standard Base64:
let bytesToEncode : [UInt8] = [1,2,3]
let base64EncodedString = SwiftyBase64.EncodeString(bytesToEncode)
or URL and Filename Safe Base64:
let bytesToEncode : [UInt8] = [1,2,3]
let base64EncodedString = SwiftyBase64.EncodeString(bytesToEncode, alphabet:.URLAndFilenameSafe)
Solution 15:[15]
@Airspeed Velocity answer in Swift 2.0:
let str = "iOS Developer Tips encoded in Base64"
print("Original: \(str)")
let base64Encoded = str.dataUsingEncoding(NSUTF8StringEncoding)!.base64EncodedStringWithOptions([])
print("Encoded: \(base64Encoded)")
let base64DecodedData = NSData(base64EncodedString: base64Encoded, options: [])!
var base64DecodedString = String(data: base64DecodedData, encoding: NSUTF8StringEncoding)!
print("Decoded: \(base64DecodedString)")
Solution 16:[16]
public extension String{
///base64EncodedString
var base64EncodedString:String{
if let data = data(using: .utf8){
return data.base64EncodedString()
}
return ""
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow