在IOS 8中跳过打印UI? [英] Skipping the Printing UI in IOS 8?
问题描述
我可以找到一些代码来跳过打印UI并通过Air Printer直接打印。但代码不完整。代码如下,
I could find some code for skipping the Printing UI and direct print through the Air Printer.But the code was incomplete. The code is below,
UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:self.savedPrinter];
[printPicker presentAnimated:YES completionHandler:
^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error) {
if (userDidSelect) {
self.savedPrinter = printerPicker.selectedPrinter;
}
}];
在此代码中,提到了 self.savedPrinter 。但是我怎样才能在这里获得保存的打印机。请给我一个完整的答案或样本解决方案。
In this code, self.savedPrinter mentioned. But how can i get the saved printer here. Please give me a complete answer or sample solution for this.
推荐答案
我得到了问题的解决方案,我在上面问过。这是解决方案,
I got the solution for the question,which i asked above.Here is the solution,
首先,你必须设置 UIPrinterPickerController 委托;即你班级中的 UIPrinterPickerControllerDelegate 。下一步是从UIPrinterPickerController中选择打印机。所以你必须在你的方法中添加一些代码。我在我的设置viewcontroller中使用按钮动作。
First of all, you have to set the UIPrinterPickerController delegate; ie UIPrinterPickerControllerDelegate in your class.The next step is select the printer from the UIPrinterPickerController. So you have to add some code in your method.Here I am using button action in my settings 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中获取打印机对象,在其他视图中。
If you want to set print functionality in any other view,You have to store the printer URL details and fetch the printer object from the stored url, in other views.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[printerURL absoluteString] forKey:@"yourKey"];
[defaults synchronize];
现在您可以通过使用保存的打印机调用printToPrinter(:completionHandler :)来告诉您的UIPrintInteractionController直接打印而不是使用现有方法之一。您可以在按钮操作中调用该方法。
Now you can tell your UIPrintInteractionController to print directly by calling printToPrinter(:completionHandler:) with the saved printer instead of using one of the present methods.You can call the method in your button action.
//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);
}
}];
}
}
}
我想,这是有帮助的你的需求。
I think, this is help your needs.
这篇关于在IOS 8中跳过打印UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!