I am using SwiftUI to build a macOS app. I have to print papers and I'm trying to see if I'm actively connected to a printer. I have a default printer set as you can see in the below image but it's currently offline. My issue is the code I have returns true that I am connected to a printer because I have a saved printer device. Is there a way to check if the printer is offline even when I'm connected?
In the image it says the printer is offline and I need to know how to get that.
Code I'm currently using that returns true:
func isConnectedToPrinter() -> Bool {
let printers = NSPrinter.printerNames
return !printers.isEmpty
}
This returns true because the printer is still remembered by my mac even though its Offline(powered down).
Any idea on how mac OS can determine the printer is "Offline"?
Also here is my current code to print the pdfDocument, Is there anything I can add here to help?
private func printPDFDocument(forDoc document: PDFDocument) {
let printInfo = NSPrintInfo.shared
printInfo.horizontalPagination = .fit
printInfo.verticalPagination = .fit
printInfo.orientation = .portrait
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.leftMargin = 0
printInfo.rightMargin = 0
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = true
let scale: PDFPrintScalingMode = .pageScaleDownToFit
let printOp = document.printOperation(for: printInfo, scalingMode: scale, autoRotate: true)
DispatchQueue.main.async {
let result = printOp?.run()
self.showLoadingText = false
}
}