Log list of available printers and their URLs

2.6k Views Asked by At

I am attempting to set up a UIPrinter instance so that my iPad app can print directly to that printer without having to present the print controller dialogue. The problem I'm having is that I can't seem to find the URL of this printer. It is connected via AirPrint.

Visiting http://localhost:631/printers/ does show the printer, but it shows the USB version of the printer's URL (i.e. usb://Brother/QL-710W?serial=12345).

What I am wondering is, how can I print (to the debug output) a list of my available printers, and their URLs? I figure by doing this I can then locate my printer's AirPrint URL and go from there.

Thanks!

3

There are 3 best solutions below

4
On BEST ANSWER

Here's a simplified version in Swift 3 for anyone stumbling upon this same question in 2017:

let pickerController = UIPrinterPickerController(initiallySelectedPrinter: nil)

pickerController.present(animated: true) { (controller, completed, error) in
    if completed == true {
        print(controller.selectedPrinter!.url)
    }
}
0
On

This might not be the best way to do it, but I ended up displaying the Printer Picker Controller, then printing (to the debug area) the URL of the selected UIPrinter:

let pickerController = UIPrinterPickerController(initiallySelectedPrinter: nil)
pickerController.presentFromRect(CGRectMake(0, 0, 300, 500), inView: self, animated: true) { (controller:UIPrinterPickerController!, completed:Bool, error:NSError!) -> Void in
    println(controller.selectedPrinter?.URL)
}

Open to suggestions if there is a better way!

0
On

Here is what I did.

Global Var

var ReceiptPrinterHolder = NSURL()
var currentPrinter: UIPrinter?
var ReceiptPrinter: UIPrinter?

func Works(){

    let printerPicker = UIPrinterPickerController(initiallySelectedPrinter: currentPrinter2)

    printerPicker.presentFromRect(CGRectMake(0, 0, 300, 500), inView: view, animated: true, completionHandler: {
        (printerPicker, userDidSelect, error) in

        if userDidSelect {
            var selectedPrinter: UIPrinter? { return printerPicker.selectedPrinter }
            currentPrinter = selectedPrinter
            self.DisplaySelectedAction()
        }else{
            print("Did not work")
        }
    })

    // return currentPrinter2!
}

@IBAction func ReceiptPrinterAction() {
    Works()

    if currentPrinter != nil {
    Label2.text = "Receipt Printer \(ReceiptPrinter!.displayName)"
         ReceiptPrinter = currentPrinter
         ReceiptPrinterHolder = ReceiptPrinter!.URL
    }
}