gpt4 book ai didi

ios - 自动 AirPrint IOS

转载 作者:行者123 更新时间:2023-11-29 12:00:41 28 4
gpt4 key购买 nike

我需要在没有用户交互的情况下在后台使用 Xcode 进行 AirPrint。我不在乎是否必须使用第 3 方框架来执行此操作。这是我的代码,但需要用户的交互。

 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];

如果有人能提供帮助那就太好了!

最佳答案

I found a video that helped a lot !这是 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");
}];

}

希望对遇到同样问题的 friend 有所帮助。 Here is more to read about what you can do with this new API !

关于ios - 自动 AirPrint IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37152468/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com