我需要在没有用户交互的情况下在Xcode中使用AirPrint。我不在乎我是否必须使用第三方框架来做到这一点。下面是我的代码,但需要用户的交互。
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];如果有人能帮忙,那就太好了!
发布于 2016-05-12 19:24:24
我找到了一段视频,很有帮助!这是iOS 8的wwdc视频,他们刚刚推出了新的打印方式,而不用UIPrintInteractionController。这就是了。
-(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");
}];
}我希望这对任何有同样问题的人都有帮助。下面是关于如何使用这个新API的更多信息!
https://stackoverflow.com/questions/37152468
复制相似问题