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 p
@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)")
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
}
}
Xcode 11.4 • Swift 5.2 or later
extension String {
var data: Data { Data(utf8) }
var base64Encoded: Data { data.base64EncodedData() }
var base64Decoded: Data? { Data(base64Encoded: self) }
}
extension Data {
var base64Decoded: Data? { Data(base64Encoded: self) }
var string: String? { String(data: 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
Swift 3 or 4
let base64Encoded = Data("original string".utf8).base64EncodedString()
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)!")
}
}
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)")
}
}