1

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!

Jody Heavener
  • 2,704
  • 5
  • 41
  • 70

3 Answers3

3

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)
    }
}
Chase McElroy
  • 383
  • 3
  • 8
  • Perfect! This really helped. – Tim Mar 02 '19 at 06:26
  • It's 2023 and I'm using Xcode 14 and swift 5 now. I'm looking for exactly the same as what's in the original post, but the solution no longer seems to work. I can't get a UIPrinterPickerController to present using the above code. No errors, just nothing happens when I tap the button in my view. Any ideas how this code may have changed since Swift 3? – atoss Aug 24 '23 at 02:43
1

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!

Jody Heavener
  • 2,704
  • 5
  • 41
  • 70
0

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
    }
}
spenibus
  • 4,339
  • 11
  • 26
  • 35