Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

objective c - Skipping the Printing UI in IOS 8?

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the solution for the question,which i asked above.Here is the solution,

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;

        }
      }];
   }
}

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...