我得到了问题的解决方案,我在上面问过。这是解决方案,
首先,您必须设置UIPrinterPickerController委托;即你的类中的UIPrinterPickerControllerDelegate。下一步是从UIPrinterPickerController 中选择打印机。所以你必须在你的方法中添加一些代码。这里我在我的设置视图控制器中使用按钮操作。
- (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 直接打印,而不是使用现有方法之一。您可以在按钮操作中调用该方法。
//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);
}
}];
}
}
}
我认为,这是帮助您的需求。