Reputation: 91
I need to AirPrint in Xcode in the background without users interaction. I do not care if I have to use a 3rd party framework to do this. Here is the code I have but requires the user's interaction.
NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@",TextField.text];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"PrintJob";
pic.printInfo = printInfo;
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0);
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
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);
}
};
[pic presentAnimated:YES completionHandler:completionHandler];
If any one could help that would be great!
Upvotes: 1
Views: 639
Reputation: 91
I found a video that helped a lot! It was the wwdc video for iOS 8 they just introduced new way to print without using the UIPrintInteractionController. Here it is.
-(IBAction)print:(id)sender{
NSMutableString *printBody = [NSMutableString stringWithFormat:@"Hey this is a test lets hope it works!"];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"PrintJob";
pic.printInfo = printInfo;
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0);
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
pic.showsPageRange = YES;
//save your printer using code also in video.
UIPrinter *SavedPrinterUserDefaults = [[NSUserDefaults standardUserDefaults]
objectForKey:@"SavedPrinter"];
[pic printToPrinter:SavedPrinterUserDefaults
completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
if (completed && !error)
NSLog(@"YAYA! it printed");
}];
}
I hope this helps any one who has the same problem. Here is more to read about what you can do with this new API!
Upvotes: 1