Biks
Biks

Reputation: 105

iOS Airprint without using Printer Selection interface

I am making a app where the user selects the default printer first and then onwards when clicking the Print button, the photo gets printed automatically without any dialogue box. Below is my code. I am getting an error "PrintViewController has no initializers"

class PrintViewController: UIViewController {

var defaultPrinter : UIPrinter

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


func SelecteDefaultPrinter(){
    let printerPicker = UIPrinterPickerController(initiallySelectedPrinter: nil)
    printerPicker.present(from: CGRect(x: 0, y: 0, width: 600, height: 500), in: self.view, animated: true) { (printerPicker, userDidSelect, error) in
        if userDidSelect {
            //Here get the selected UIPrinter
            self.defaultPrinter = printerPicker.selectedPrinter!
        }
    }
}


@IBAction func Button_DefaultPrinter(_ sender: Any) {
    SelecteDefaultPrinter()
}

//In your view controller
@IBAction func printButton(sender: AnyObject) {

    let printInfo = UIPrintInfo(dictionary:nil)
    printInfo.outputType = UIPrintInfoOutputType.general
    printInfo.jobName = "Test Print"

    // Set up print controller
    let printController = UIPrintInteractionController.shared
    printController.printInfo = printInfo

    // Assign a UIImage version of my UIView as a printing iten
    printController.printingItem = self.view.toImage()

    // Assign a Specific Image to printing item
    //printController.printingItem = customImageView.image

    // Print With Dialogue Box
    //printController.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil)


    // Print Without Dialogue Box
    printController.print(to: defaultPrinter, completionHandler: {(controller, success, error) -> Void in
        if success {
            debugPrint("Printing Completed.")
        } else {
            debugPrint("Printing Failed.")
        }
    })
}

Upvotes: 2

Views: 1085

Answers (1)

Biks
Biks

Reputation: 105

Solved! This was the mistake

var defaultPrinter : UIPrinter!

It was eating my brain. :D

Upvotes: 3

Related Questions