更新:
UIPrinterPickerController 来自UIKit,因此无法将“打印”进程推到后台并退出主 UI 线程。
在当前的UIPrintInteractionController.PrintToPrinter 实现中(当前最高为 iOS 10.3 B4),没有公开的方法来禁用打印进度(连接、准备等)警报/对话框(带取消按钮)或修改它的外观。
此接口是使用AirPrint 的高级包装器,因此使用较低级别的 Internet 打印协议 (IPP) 来执行实际打印、打印机上的作业队列监控等... IPP 目前未公开公开iOS 中可用的框架...
允许后台打印的程序未使用UIPrintInteractionController 进行打印。 大多数确实使用UIPrinterPickerController 从用户那里获得UIPrinter 选择,然后使用UIPrinter.Url.AbsoluteUrl 通过HTTP/HTTPS Post/Get 直接与打印机“对话”。根据所使用的打印机,与 IPP 甚至 USB/串行直接连接的打印机相比,基于 TCP 的套接字也是一种选择。
回复:https://en.wikipedia.org/wiki/Internet_Printing_Protocol
原文:
选择打印机:
if (allowUserToSelectDifferentPrinter || printerUrl == null)
{
UIPrinter uiPrinter = printerUrl != null ? null as UIPrinter : UIPrinter.FromUrl(new NSUrl(printerUrl));
var uiPrinterPickerController = UIPrinterPickerController.FromPrinter(uiPrinter);
uiPrinterPickerController.Present(true, (printerPickerController, userDidSelect, error) =>
{
if (userDidSelect)
{
uiPrinter = uiPrinterPickerController?.SelectedPrinter;
printerUrl = uiPrinter.Url.AbsoluteUrl.ToString();
Console.WriteLine($"Save this UIPrinter's Url string for later use: {printerUrl}");
}
});
}
使用UIPrintInteractionController 和现有的UIPrinter 打印:
if (printerUrl != null)
{
// re-create a UIPrinter from a saved NSUrl string
var uiPrinter = UIPrinter.FromUrl(new NSUrl(printerUrl));
var printer = UIPrintInteractionController.SharedPrintController;
printer.ShowsPageRange = false;
printer.ShowsNumberOfCopies = false;
printer.ShowsPaperSelectionForLoadedPapers = false;
var printInfo = UIPrintInfo.PrintInfo;
printInfo.OutputType = UIPrintInfoOutputType.General;
printInfo.JobName = "StackOverflow Print Job";
var textFormatter = new UISimpleTextPrintFormatter("StackOverflow Rocks")
{
StartPage = 0,
ContentInsets = new UIEdgeInsets(72, 72, 72, 72),
MaximumContentWidth = 6 * 72,
};
printer.Delegate = new PrintInteractionControllerDelegate();
printer.PrintFormatter = textFormatter;
printer.PrintToPrinter(uiPrinter, (printInteractionController, completed, error) =>
{
if ((completed && error != null))
{
Console.WriteLine($"Print Error: {error.Code}:{error.Description}");
PresentViewController(
UIAlertController.Create("Print Error", "Code: {error.Code} Description: {error.Description}", UIAlertControllerStyle.ActionSheet),
true, () => { });
}
printInfo?.Dispose();
uiPrinter?.Dispose();
uiPrinter.
});
}
else
{
Console.WriteLine("User has not selected a printer...printing disabled");
}