Printing to Airprint using UIPrintInteractionController

Feature request :
37516 - iOS printing to airprint should be supported
Status: Needs Review Rank: Not Ranked Product: Xojo Category: Compiler
Printing is a basic feature necessary for most business applications. Please add that.
feedback://showreport?report_id=37516


I have been trying to understand how to print with declares from Xojo iOS, since printing is not yet supported.

Thanks to the brilliant work of Jason King, I can use the UIPrintInteractionController class.

I found this example at http://stackoverflow.com/questions/4373420/how-to-print-in-ios-4-2 that seems short enough to be translated to Xojo. But am far too ignorant in Objective-C :

[code]- (IBAction)printContent:(id)sender {
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.documentName;
pic.printInfo = printInfo;

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]
                                             initWithText:yourNSStringWithContextOfTextFileHere];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
[textFormatter release];
pic.showsPageRange = YES;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    if (!completed && error) {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
} else {
    [pic presentAnimated:YES completionHandler:completionHandler];
}

}[/code]

I need insight from Objective-C specialists here. Do you think it is possible to adapt this code to Xojo ?

So I created a line for line copy of it in Xojo but it doesn’t seem to work as expected. On my printer it prints out a blank sheet of paper with no text whatsoever. Maybe someone else will look at it to try and see where I have gone wrong?
https://www.dropbox.com/s/68eid2gq7coac9l/Plain%20text%20printing%20ios.xojo_binary_project?dl=0

[quote=155055:@Jason King]So I created a line for line copy of it in Xojo but it doesn’t seem to work as expected. On my printer it prints out a blank sheet of paper with no text whatsoever. Maybe someone else will look at it to try and see where I have gone wrong?
https://www.dropbox.com/s/68eid2gq7coac9l/Plain%20text%20printing%20ios.xojo_binary_project?dl=0[/quote]

Jason, I admire your mastery.

I have downloaded the project, but unfortunately, it does the same here : eject a blank page. Which is already a result ; the print dialog is shown and works fine, and the blank page means the print job has been carried out. Now we need to figure out how to inject the data to print.

I looked at https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/Printing/Printing.html but it seems the code should work. Maybe the only thing that remains is to build the Objective-C code and see if it works. Maybe it does not either. If it does, then the issue is with the Xojo code.

I tried to paste the code in a button in Xcode, but because I do not know know Objective-C I get errors I cannot correct, like self , documentName, textFormatter release. Makes me feel like a monkey in front of a computer keyboard :frowning:

I am sure it is pretty simple for someone who knows what he does, but it is beyond me.

I finally found a sample project that works, in the Apple Dev Library at https://developer.apple.com/library/ios/samplecode/PrintWebView/Introduction/Intro.html which prints the current web view using UIPrintInteractionController

So now it is probably possible to compare the existing code with the Apple sample and maybe find why the Xojo project does not print ?