【发布时间】:2025-12-04 02:25:02
【问题描述】:
我需要在后台在 Xcode 中进行 AirPrint,无需用户交互。我不在乎是否必须使用 3rd 方框架来执行此操作。这是我拥有但需要用户交互的代码。
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];
如果有人能帮忙那就太好了!
【问题讨论】:
-
您想在不执行任何操作的情况下自动打印?
-
是的@DarjiJigar 这就是我想做的事
-
@Connor 你有没有在 viewdidload 方法中尝试这段代码
标签: ios objective-c printing airprint