在 IOS 8 中跳过打印 UI?
Skipping the Printing UI in IOS 8?
我可以找到一些代码来跳过打印 UI 并通过 Air 直接打印 Printer.But 代码不完整。代码如下,
UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:self.savedPrinter];
[printPicker presentAnimated:YES completionHandler:
^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error) {
if (userDidSelect) {
self.savedPrinter = printerPicker.selectedPrinter;
}
}];
在此代码中,提到了self.savedPrinter。但是我怎样才能在这里得到保存的打印机。请给我一个完整的答案或示例解决方案。
我得到了我问的问题的答案above.Here是答案,
首先,你必须设置UIPrinterPickerController委托;即 UIPrinterPickerControllerDelegate 在您的 class.The 下一步是 select 来自 UIPrinterPickerController 的打印机。所以你必须在你的method.Here中添加一些代码我在我的设置中使用按钮操作viewcontroller。
- (IBAction)btnSettingsTapped:(id)sender
{
//Search printer method.
[self searchForPrinters];
}
- (void) searchForPrinters
{
// For checking the ios version is greater than ios 7.Because skipping the Printing UI is ony in ios8 and later.
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
[printPicker presentAnimated:YES completionHandler:
^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
{
if (userDidSelect)
{
//User selected the item in the UIPrinterPickerController and got the printer details.
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
//Here you will get the printer and printer details.ie,
// printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
[btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
NSURL *printerURL = printerPicker.selectedPrinter.URL;
}
}];
}
}
如果您想在任何其他视图中设置打印功能,您必须在其他视图中存储打印机 URL 详细信息并从存储的 url 中获取打印机对象。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[printerURL absoluteString] forKey:@"yourKey"];
[defaults synchronize];
现在您可以通过使用保存的打印机调用 printToPrinter(:completionHandler:) 来告诉您的 UIPrintInteractionController 直接打印,而不是使用现有打印机之一 methods.You 可以在您的按钮操作中调用该方法。
//Printing item details passed to this method,
-(void)printYourItem :(NSData*)data
{
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
UIPrinter *currentPrinterObj = [UIPrinter printerWithURL:[NSURL URLWithString:[userDefault stringForKey:@"yourKey"]]];
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if(currentPrinterObj)
{
[controller printToPrinter:currentPrinterObj completionHandler:^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
{
if(completed)
{
}
else
{
NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
}
}];
}
}
}
我想,这正是您需要的帮助。
我可以找到一些代码来跳过打印 UI 并通过 Air 直接打印 Printer.But 代码不完整。代码如下,
UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:self.savedPrinter];
[printPicker presentAnimated:YES completionHandler:
^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error) {
if (userDidSelect) {
self.savedPrinter = printerPicker.selectedPrinter;
}
}];
在此代码中,提到了self.savedPrinter。但是我怎样才能在这里得到保存的打印机。请给我一个完整的答案或示例解决方案。
我得到了我问的问题的答案above.Here是答案,
首先,你必须设置UIPrinterPickerController委托;即 UIPrinterPickerControllerDelegate 在您的 class.The 下一步是 select 来自 UIPrinterPickerController 的打印机。所以你必须在你的method.Here中添加一些代码我在我的设置中使用按钮操作viewcontroller。
- (IBAction)btnSettingsTapped:(id)sender
{
//Search printer method.
[self searchForPrinters];
}
- (void) searchForPrinters
{
// For checking the ios version is greater than ios 7.Because skipping the Printing UI is ony in ios8 and later.
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
[printPicker presentAnimated:YES completionHandler:
^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
{
if (userDidSelect)
{
//User selected the item in the UIPrinterPickerController and got the printer details.
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
//Here you will get the printer and printer details.ie,
// printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
[btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
NSURL *printerURL = printerPicker.selectedPrinter.URL;
}
}];
}
}
如果您想在任何其他视图中设置打印功能,您必须在其他视图中存储打印机 URL 详细信息并从存储的 url 中获取打印机对象。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[printerURL absoluteString] forKey:@"yourKey"];
[defaults synchronize];
现在您可以通过使用保存的打印机调用 printToPrinter(:completionHandler:) 来告诉您的 UIPrintInteractionController 直接打印,而不是使用现有打印机之一 methods.You 可以在您的按钮操作中调用该方法。
//Printing item details passed to this method,
-(void)printYourItem :(NSData*)data
{
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
UIPrinter *currentPrinterObj = [UIPrinter printerWithURL:[NSURL URLWithString:[userDefault stringForKey:@"yourKey"]]];
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if(currentPrinterObj)
{
[controller printToPrinter:currentPrinterObj completionHandler:^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
{
if(completed)
{
}
else
{
NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
}
}];
}
}
}
我想,这正是您需要的帮助。