Generate QR Codes with Swift
Swift provides built-in QR code generation through CoreImage's CIQRCodeGenerator filter. No third-party dependencies needed for iOS and macOS apps.
Setup
CoreImage is included in iOS and macOS SDKs. No additional installation required.
import CoreImage // No installation needed — included in iOS & macOS SDKGenerate QR Codes with CoreImage
Code examples using Swift's built-in CIQRCodeGenerator.
import CoreImage
import UIKit
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: .ascii)
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
guard let ciImage = filter.outputImage else { return nil }
// Scale up for crisp rendering
let transform = CGAffineTransform(scaleX: 10, y: 10)
let scaledImage = ciImage.transformed(by: transform)
return UIImage(ciImage: scaledImage)
}
let qrImage = generateQRCode(from: "https://qrcode.fun")func generateColoredQRCode(from string: String,
foreground: UIColor = .black,
background: UIColor = .white) -> UIImage? {
let data = string.data(using: .ascii)
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator"),
let colorFilter = CIFilter(name: "CIFalseColor") else { return nil }
qrFilter.setValue(data, forKey: "inputMessage")
qrFilter.setValue("H", forKey: "inputCorrectionLevel")
colorFilter.setValue(qrFilter.outputImage, forKey: "inputImage")
colorFilter.setValue(CIColor(color: foreground), forKey: "inputColor0")
colorFilter.setValue(CIColor(color: background), forKey: "inputColor1")
guard let output = colorFilter.outputImage else { return nil }
let transform = CGAffineTransform(scaleX: 10, y: 10)
return UIImage(ciImage: output.transformed(by: transform))
}Generate QR Codes via API in Swift
Call the QRCode.fun API from Swift using URLSession for styled QR codes with custom colors and logos.
import Foundation
func generateQRCodeViaAPI(data: String) async throws -> Data {
let url = URL(string: "https://qrcode.fun/api/generate-qr-styled")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let payload: [String: Any] = [
"data": data,
"width": 300,
"height": 300,
"type": "png",
"margin": 10,
"dotsOptions": ["color": "#1A2B3C", "type": "rounded"],
"cornersSquareOptions": ["color": "#8564C3", "type": "extra-rounded"],
"backgroundOptions": ["color": "#FFFFFF"]
]
request.httpBody = try JSONSerialization.data(withJSONObject: payload)
let (responseData, _) = try await URLSession.shared.data(for: request)
return responseData // Parse JSON to get base64 image
}
// Usage
let imageData = try await generateQRCodeViaAPI(data: "https://qrcode.fun")Live QR Code Preview
Try generating a QR code with Swift right now.
CoreImage vs API
Compare using CoreImage directly versus the QRCode.fun API.
| Feature | CoreImage | QRCode.fun API |
|---|---|---|
| Setup complexity | Built-in, no install needed | HTTP request via URLSession |
| Customization | Basic (tinting with CIFilter) | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | Part of iOS/macOS SDK | Always up-to-date |
| Output formats | CIImage → UIImage/NSImage | PNG, SVG |
Swift QR Code Use Cases
Common scenarios for QR codes in Swift applications.
iOS Apps
Generate QR codes for sharing links, contacts, Wi-Fi credentials, and app deep links in iPhone and iPad apps.
SwiftUI Views
Create reusable SwiftUI components that render QR codes with custom styling and animations.
macOS Apps
Build macOS tools for generating QR codes for business cards, labels, and document management.
Apple Watch
Display QR codes on Apple Watch for quick access to boarding passes, tickets, and loyalty cards.
Frequently Asked Questions
Common questions about generating QR codes with Swift.
Start generating QR codes with Swift
Use our free generator or integrate the API into your iOS and macOS apps.