How to Generate EFQRCode for tvOS and watchOS: A Complete Implementation Guide
You can generate EFQRCode for tvOS and watchOS using the same EFQRCode.Generator class from the core library, calling toImage(width:) to produce a UIImage that you display via UIKit on tvOS or assign to a WKInterfaceImage on watchOS.
EFQRCode is a pure-Swift QR code library maintained by efprefix that targets iOS, macOS, tvOS, watchOS, and visionOS from a single conditional codebase. The platform-agnostic generator lives in Source/EFQRCode.swift, allowing you to generate EFQRCode for tvOS or watchOS without platform-specific forks or API differences.
Platform Requirements and Package Setup
EFQRCode requires Swift 5 or later and supports tvOS 13+ and watchOS 6+. Add the package to your Xcode project via Swift Package Manager, then import the module in your source files:
import EFQRCode
The library automatically handles platform differences through conditional compilation, exposing the same public API across all Apple platforms.
Generating QR Codes on tvOS
On tvOS, EFQRCode operates exactly as it does on iOS. You instantiate an EFQRCode.Generator, configure content and error correction levels, and render the output to a UIImage using toImage(width:).
The reference implementation resides in Examples/tvOS/tvOS Example/GeneratorController.swift, which demonstrates style selection, image generation, and presentation in a view controller:
import UIKit
import EFQRCode
class QRGeneratorViewController: UIViewController {
var qrImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
qrImageView = UIImageView()
qrImageView.contentMode = .scaleAspectFit
view.addSubview(qrImageView)
qrImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
qrImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
qrImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
qrImageView.widthAnchor.constraint(equalToConstant: 300),
qrImageView.heightAnchor.constraint(equalTo: qrImageView.widthAnchor)
])
generateQRCode()
}
private func generateQRCode() {
do {
let generator = try EFQRCode.Generator(
"https://github.com/efprefix/efqrcode",
errorCorrectLevel: .h,
style: EFQRCodeStyle.image(params: .init())
)
let image = try generator.toImage(width: 300)
qrImageView.image = image
} catch {
print("⚠️ QR generation failed: \(error)")
}
}
}
For animated QR codes on tvOS, call toGIFData(width:) instead, which returns Data suitable for UIImage.animatedImage or direct playback.
Generating QR Codes on watchOS
When you generate EFQRCode for watchOS, the API remains identical, but the UI layer shifts to WatchKit. The watchOS example in Examples/iOS/watchOS Example Extension/InterfaceController.swift shows two patterns: receiving a pre-generated UIImage from the paired iPhone app via context, or generating directly on the watch device.
Use WKInterfaceImage.setImage(_:) to display the result:
import WatchKit
import EFQRCode
class QRInterfaceController: WKInterfaceController {
@IBOutlet var qrImage: WKInterfaceImage!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
// Pattern 1: Receive UIImage from iPhone app via context
if let img = context as? UIImage {
qrImage.setImage(img)
} else {
// Pattern 2: Generate directly on watch (watchOS 6+)
do {
let gen = try EFQRCode.Generator(
"Watch QR Demo",
errorCorrectLevel: .m
)
let img = try gen.toImage(width: 200)
qrImage.setImage(img)
} catch {
print("⚠️ Generation error: \(error)")
}
}
}
}
Note that watchOS cannot display animated GIFs directly via WKInterfaceImage, so toGIFData(width:) is typically reserved for tvOS and iOS implementations.
Core Implementation Details
According to the EFQRCode source code, the generator relies on platform-specific extensions defined in Source/Extension/UIImage+EFQRCode.swift to render the QR matrix into a UIImage on tvOS and watchOS. The EFQRCode.Generator class in Source/EFQRCode.swift accepts parameters including:
- Content: The string or data to encode
- errorCorrectLevel: Error correction level (
.l,.m,.q,.h) - style: Optional styling parameters for colors, icons, or watermarks
Both platforms share this underlying implementation; only the UI presentation layer differs between UIKit and WatchKit.
Summary
- EFQRCode supports tvOS 13+ and watchOS 6+ from the same Swift package without platform-specific branches.
- Use
try EFQRCode.Generator(content, errorCorrectLevel: .h)to instantiate the generator as implemented inSource/EFQRCode.swift. - Call
toImage(width:)to generate a staticUIImagesuitable for both tvOSUIImageViewand watchOSWKInterfaceImage. - Reference
Examples/tvOS/tvOS Example/GeneratorController.swiftfor tvOS UI patterns andExamples/iOS/watchOS Example Extension/InterfaceController.swiftfor watchOS integration. - Animated QR codes via
toGIFData(width:)work on tvOS but are not directly displayable on watchOS.
Frequently Asked Questions
Can I generate animated QR codes on watchOS?
No. While you can call toGIFData(width:) on watchOS, the platform does not support displaying animated GIFs directly in WKInterfaceImage. For watchOS, generate static QR codes using toImage(width:) or handle animation frames manually through WKImageAnimation.
What is the minimum watchOS version required to generate EFQRCode locally?
watchOS 6 or later is required to generate EFQRCode directly on the Apple Watch using Swift 5. Earlier versions must receive pre-generated images from the paired iPhone app via Watch Connectivity.
Do I need separate code for tvOS and iOS implementations?
No. The EFQRCode.Generator API is identical across platforms. The only differences are in the UI framework: use UIImageView for tvOS (UIKit) and WKInterfaceImage for watchOS (WatchKit), as shown in the respective example projects.
How do I pass a QR code image from my iPhone app to Apple Watch?
Generate the UIImage on the iPhone using toImage(width:), then pass it as context when presenting the watch interface controller via presentController(withName:context:). In the watch extension's awake(withContext:) method, cast the context to UIImage and call setImage(_:) on your WKInterfaceImage outlet.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →