The project is dual licensed
- Public Domain
- MIT
Use it under whichever of these two licensing options that are allowed in your country and you feel the most comfortable with.
IMPORTANT If you have access to Swift libraries, use the built-in Base64 functions. They are faster in most cases.
In your Package.swift, add
import PackageDescription
let package = Package(
name: "MyApp",
targets: [],
dependencies: [
.Package(url: "https://github.com/ekscrypto/Base64.git", .upToNextMajor(from: "1.2.2"))
]
)
Add the following line to your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'Base64'
end
NSString *helloWorld = @"Hello World";
NSString *helloInBase64 = [helloWorld base64String];
NSString *helloDecoded = [NSString stringFromBase64String:helloInBase64];
NOT RECOMMENDED: If you have access to the built-in base64 functions offered by Swift, use them. They are much faster.
Using MF_Base64codec directly
let data = "Hello World".data(using: .utf8)!
let base64 = MF_Base64Codec.base64String(from: data) // SGVsbG8gV29ybGQ=
let querySafeBase64 = MF_Base64Codec.base64UrlEncodedString(fromBase64String: base64) // SGVsbG8gV29ybGQ
Using NSData objects
let data = "Hello World".data(using: .utf8)! as NSData
let base64 = data.base64String() // SGVsbG8gV29ybGQ=
let querySafeBase64 = data.base64UrlEncodedString() // SGVsbG8gV29ybGQ
- Encoding: 5% slower than Swift's built-in Base64 encoding
- Encoding: 4 to 5 times faster than using the equivalent SecTransform.
- Encoding: 30% faster than https://github.com/l4u/NSData-Base64 (metric from 2012)
- Decoding: 4 times slower than Swift's built-in Base64 decoding
- Decoding: 5% faster than using the equivalent SecTransform.
- Decoding: 5% faster than https://github.com/l4u/NSData-Base64 (metric from 2012)
Performance metrics are based on evaluation done in 2022 using macOS, Xcode 13.2.1 and Swift 5.5.2 on a 2.2 GHz 6-Core Intel Core i7 MacBook Pro.
- Compile with Automatic Reference Counting
- Compatible with Mac OSX 10.9+ and iOS 4.0+
- Implemented as per RFC 4648, see http://www.ietf.org/rfc/rfc4648.txt for more details.