// // FunctionTestViewController.m // iSales-NPD // // Created by Ray on 3/15/16. // Copyright © 2016 United Software Applications, Inc. All rights reserved. // #import "FunctionTestViewController.h" #import "RAUtils.h" #import "AppDelegate.h" @interface FunctionTestViewController () @end @implementation FunctionTestViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.airPrinterController = [UIPrintInteractionController sharedPrintController]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[self navigationController] setNavigationBarHidden:NO animated:NO]; self.filename = @"print_test"; NSData* data =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"I25418359" ofType:@"pdf" ]]; [self startAirPrintWithData1:data]; } - (void)startAirPrintWithData:(id )data { UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.orientation = UIPrintInfoOrientationPortrait; printInfo.jobName = @"CoolVisitAirPrint"; self.airPrinterController.printInfo = printInfo; self.airPrinterController.printingItem = data; self.airPrinterController.delegate = self; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if(completed && error) NSLog(@"Printing failed due to error in domain %@ with error code %lu. Localized description: %@, and failure reason: %@", error.domain, (long)error.code, error.localizedDescription, error.localizedFailureReason); }; UIPrinter *airPrinter = [UIPrinter printerWithURL:self.printerURL];///*self.printer;*/ [[UIPrinter printerWithURL:self.printerURL] contactPrinter:^(BOOL available) { if (available) { [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler]; NSLog(@"AIRPRINTER AVAILABLE"); } else { NSLog(@"AIRPRINTER NOT AVAILABLE"); } }]; // [airPrinter contactPrinter:^(BOOL available) // { // if (available) { // self.printerdisplayName= airPrinter.displayName; //// self.printerNameLabel.text = self.savedPrinter.displayName; //// self.printerLocationLabel.text = self.savedPrinter.displayLocation; // } // else { // NSLog(@"cannot connect to printer"); //// self.printerConnectionGoneIndicator.hidden = NO; // } // }]; // [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler]; } - (void)startAirPrintWithData1:(id )data { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"preparing print..."]; UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController]; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.orientation = UIPrintInfoOrientationPortrait; printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]; if(self.filename.length>0) printInfo.jobName = self.filename; airPrinterController.printInfo = printInfo; airPrinterController.printingItem = data; airPrinterController.delegate = self; airPrinterController.showsNumberOfCopies=true; airPrinterController.showsPageRange = true; airPrinterController.showsPaperSelectionForLoadedPapers=true; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if(completed && error) { // DebugLog(@"Printing failed due to error in domain %@ with error code %lu. Localized description: %@, and failure reason: %@", error.domain, (long)error.code, error.localizedDescription, error.localizedFailureReason); } }; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.printerURL==nil) { [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) { UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil]; // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler: // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler: [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler: // [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. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString; [self startAirPrintWithData1:data]; } else { // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout) // { // // [((MainViewController*)appDelegate.main_vc) Loginout:false]; // } // [self prepareReturn:nil]; //[self.navigationController popViewControllerAnimated:true]; } }]; } } else { UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/ [airPrinter contactPrinter:^(BOOL available) { [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if (available&& appDelegate.printerURL.length>0) { [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler]; DebugLog(@"AIRPRINTER AVAILABLE"); } else { appDelegate.printerURL = nil; // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler: // // // // // [printPicker presentAnimated:YES completionHandler: // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error) // { // if (userDidSelect) // { // NSString* sid= printerController.printInfo.printerID; // UIPrintPaper*paper= printerController.printPaper; // // } // }]; DebugLog(@"AIRPRINTER NOT AVAILABLE"); if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) { UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil]; // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler: // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler: [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler: // [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. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString; [self startAirPrintWithData:data]; } else { appDelegate.printerURL = nil; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout) // { // // [((MainViewController*)appDelegate.main_vc) Loginout:false]; // } // [self prepareReturn:nil]; //[self.navigationController popViewControllerAnimated:true]; } }]; } } }]; } } //-(void)settingPrinter //{ // UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil]; // CGRect rect; // UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation; // rect = CGRectMake(0, 0, 200, 200); // void (^completionHandler)(UIPrinterPickerController *, BOOL, NSError *) = ^(UIPrinterPickerController *printController, BOOL userDidSelect, NSError * err) { // if (userDidSelect) // { // // save the urlString and Printer name, do your UI interactions // self.printerurl = pickerController.selectedPrinter.URL.absoluteString; // self.printerdisplayName = pickerController.selectedPrinter.displayName; // } // }; // // // [pickerController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler]; // // [pickerController presentAnimated:YES completionHandler:completionHandler]; //} - (IBAction)OnPrintParamsClick:(id)sender { } - (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 presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler: // [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. self.labelPrintername.text =printerPicker.selectedPrinter.displayName; // [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal]; self.printer = printerPicker.selectedPrinter; self.printerurl =printerPicker.selectedPrinter.URL.absoluteString; self.printerURL = printerPicker.selectedPrinter.URL; } }]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)onBackgroundprintClick:(id)sender { //[self startAirPrintWithImage:[UIImage imageNamed:@"notfound_s"]]; } - (IBAction)onPrintersettingClick:(id)sender { //[self settingPrinter]; [self searchForPrinters]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList { ; // (int) width = 29700 // Printing description of (*( (int *)0x7caa3a40)): // (int) height = 42000 //设置纸张大小 for(UIPrintPaper* paper in paperList) { if (CGSizeEqualToSize(paper.paperSize, CGSizeMake(612, 792))) return paper; } CGSize paperSize = CGSizeMake(612, 792); UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList]; return p; } - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController { } - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController { } - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController { } - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController { } - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController { } - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController { } @end