// // 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" #import "SignatureViewController.h" @interface FunctionTestViewController () @end @implementation FunctionTestViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.airPrinterController = [UIPrintInteractionController sharedPrintController]; } - (IBAction)onSignature:(id)sender { // SignatureViewController * signvc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"]; // signvc.title = [item_json valueForKey:@"aname"]; // signvc.indexPath = indexPath; // // signvc.delegate = self; // CommonEditorCellSignature* cell = (CommonEditorCellSignature*) [self.editorTable cellForRowAtIndexPath:indexPath]; // // signvc.existSignature = cell.imageviewSignature.image ; // // // // [self.navigationController pushViewController:signvc animated:true]; __block UIImage* signimg=nil; SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"]; // __weak typeof(self) weakself = self; vc.onReturnImg = ^(UIImage* img) { signimg = img; }; [self.navigationController pushViewController:vc animated:NO]; } - (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]; [self testUI]; } -(void)testUI { // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"waiting" completion:^{ // dispatch_async(dispatch_get_global_queue(0, 0), ^{ // sleep(2); // dispatch_async(dispatch_get_main_queue(), ^{ // [waitalert dismissViewControllerAnimated:false completion:^{ // // // // [RAUtils message_box:@"title" message:@"message" completion:nil]; // }]; // // }); // }); // }]; } - (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) 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); }; UIPrinter *airPrinter = [UIPrinter printerWithURL:self.printerURL];///*self.printer;*/ [[UIPrinter printerWithURL:self.printerURL] contactPrinter:^(BOOL available) { if (available) { [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler]; DebugLog(@"AIRPRINTER AVAILABLE"); } else { DebugLog(@"AIRPRINTER NOT AVAILABLE"); } }]; } - (void)startAirPrintWithData1:(id )data { // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..." completion:^{ PopWaitAlert* pop = [RAUtils waiting_pop:@"preparing print..." completion:nil]; 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; //10.0开始始终显示 // 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 dismissViewControllerAnimated:YES completion:nil]; [pop hide]; 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 { } }]; } } else { UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/ [airPrinter contactPrinter:^(BOOL available) { // [waitalert dismissViewControllerAnimated:YES completion:nil]; [pop hide]; if (available&& appDelegate.printerURL.length>0) { [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler]; DebugLog(@"AIRPRINTER AVAILABLE"); } else { appDelegate.printerURL = nil; 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; } }]; } } }]; } // }]; } //-(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