Print dialogs for iOS in Xojo

For iOS you may miss the print dialogs in Xojo. For upcoming 24.2 version of MBS Xojo Plugins we have new classes for you: The UIPrinterPickerControllerMBS class allows to show a printer picker dialog to choose the printer. And the UIPrintInteractionControllerMBS allows to show a dialog for printing a PDF or image.

Pick Printer

First we want to pick a printer. Let’s show the printer picker. If you like, you can set a few properties like the SelectedPrinter property to preselect a printer.

Sub PickPrinter()
	picker = New MyUIPrinterPickerControllerMBS				
	picker.Present
End Sub

In the Completed event, you can react to the user’s choice. Check the error property for an error and the userDidSelect property. If the userDidSelect property is true, the check the selectedPrinter property.

Class MyUIPrinterPickerControllerMBS Inherits UIPrinterPickerControllerMBS
	Sub Completed(userDidSelect as Boolean, error as NSErrorMBS)
		System.DebugLog CurrentMethodName+" "+userDidSelect.ToString

		If error <> Nil Then
			MessageBox error.LocalizedDescription
		End If

		If userDidSelect Then
			Dim p As UIPrinterMBS = Me.selectedPrinter
	
			If p <> Nil Then
		
				MainScreen.printer = p
				MessageBox "You picked "+p.displayName+EndOfLine+p.printerID
		
			End If
		End If
	End Sub
End Class

Print with dialog

Now let’s print something. The UIPrintInfoMBS class allows you to set a few properties. Like we may ask for grayscale printing, duplex on the long edge and pre-pick a printer. We can also decide whether to show controls for orientation, number of copies and paper choice.

To print an elements, you assign it to the printingItem property. You can print pictures, image files or PDF documents here. If you have multiple PDF documents, you can call setPrintingItems() to pass multiple items to print.

Sub Pressed()
	printController = New MyUIPrintInteractionControllerMBS
	
	Dim printinfo As New UIPrintInfoMBS
	printinfo.outputType = printinfo.OutputGrayscale
	printinfo.duplex = printinfo.DuplexLongEdge
	printinfo.jobName = "testing"
	printinfo.printerID = "Laserdrucker._ipp._tcp.local."
	
	printController.showsNumberOfCopies = True
	printController.printInfo = printinfo
	printController.printingItem = LogoImageData
	
	printController.Present
End Sub

Once you show the printing dialog, you may later see a Completed event called. Here the system tells you whether the job is completed and what error may have happened. We can lookup what printer was used and remember it for the next time.

Class MyUIPrintInteractionControllerMBS Inherits UIPrintInteractionControllerMBS
	Sub Completed(completed as Boolean, error as NSErrorMBS)
		System.DebugLog CurrentMethodName+" "+completed.ToString

		If error <> Nil Then
			MessageBox error.LocalizedDescription
		End If

		Dim printInfo As UIPrintInfoMBS = Self.printInfo

		If printInfo <> Nil Then
			System.DebugLog printInfo.printerID
		End If
	End Sub
End Class

Print without dialog

If you like to print directly to a printer on the local network for e.g. a receipt, you can use the UIPrintInteractionControllerMBS class with the printToPrinter method. Now you need the printer referenced with an UIPrinterMBS object based on the URL of the printer. This prints silently in the background and later calls the Completed event.

Sub Pressed()
	printController = New MyUIPrintInteractionControllerMBS
	printer = UIPrinterMBS.printerWithURL("ipp://Laserdrucker.local.:631/ipp/print")
	
	Dim printinfo As New UIPrintInfoMBS
	printinfo.outputType = printinfo.OutputGrayscale
	printinfo.duplex = printinfo.DuplexLongEdge
	printinfo.jobName = "testing"
	
	printController.printingItem = LogoImageData
	printController.printInfo = printinfo
	
	Dim r As Boolean = printController.printToPrinter(printer)
	
	If Not r Then
		MessageBox "can't print to this printer?"
	End If
End Sub

For all this controllers, please keep the references to them in a property of the current screen or a global one. If the controller object gets destroyed too early the dialog may not show at all or suddenly disappear.

Please try the 24.2 version of MBS Xojo iOS Plugin and see if this works for you. Please don’t hesitate to contact us with your questions.

4 Likes