2

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.")
        }
    })
}

1 Answer 1

3

Solved! This was the mistake

var defaultPrinter : UIPrinter!

It was eating my brain. :D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.