// // 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" @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]; } - (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) downloadpdfUI { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Loading..."]; NSString* url = @"http://192.168.0.119:8080/site/u/NPD/file/20160315/96ad77c5-e8cd-4bf1-ac97-21c76b56af79.pdf"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5]; //设置请求方式为get [request setHTTPMethod:@"GET"]; //添加用户会话id [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"]; //连接发送请求 NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData* content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; // self.filename = [urlResponse suggestedFilename]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; // self.navigationItem.title = self.save_name; // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200]; // self.content=nil; if(content!=nil&&content.length>0) { UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.orientation = UIPrintInfoOrientationPortrait; printInfo.jobName = @"CoolVisitAirPrint"; self.airPrinterController.printInfo = printInfo; self.airPrinterController.printingItem = content; self.airPrinterController.delegate = self; self.airPrinterController.showsNumberOfCopies=true; self.airPrinterController.showsPageRange = true; self.airPrinterController.showsPaperSelectionForLoadedPapers=true; [self.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) { UIPrintPaper*paper= printerController.printPaper; // printerController.printPaper = paper; // printer // //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.printerurl =printerPicker.selectedPrinter.URL.absoluteString; // NSURL *printerURL = printerPicker.selectedPrinter.URL; // } }]; //[self startAirPrintWithData:content]; // [reF removeFromSuperview]; // // // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *path = NSTemporaryDirectory(); // NSString *filePath = [path stringByAppendingPathComponent:self.filename]; // // NSError *error = [[NSError alloc] init]; // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; // // NSAssert(bo,@"创建目录失败"); // [self.content writeToFile:filePath atomically:YES]; // // // self.btnshare.enabled =true; // // // self.btnemail.enabled =true; // self.btnsave.enabled =true; // // if(self.onLoadSuccess) // self.onLoadSuccess(self.url); // int a=0; } else { [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self]; // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding]; } ; // NSLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]); // // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil]; }); }); } -(void) downloadpdf { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Loading..."]; NSString* url = @"http://192.168.0.119:8080/site/u/NPD/file/20160315/96ad77c5-e8cd-4bf1-ac97-21c76b56af79.pdf"; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5]; //设置请求方式为get [request setHTTPMethod:@"GET"]; //添加用户会话id [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"]; //连接发送请求 NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData* content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; // self.filename = [urlResponse suggestedFilename]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; // self.navigationItem.title = self.save_name; // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200]; // self.content=nil; if(content!=nil&&content.length>0) { [self startAirPrintWithData:content]; // [reF removeFromSuperview]; // // // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *path = NSTemporaryDirectory(); // NSString *filePath = [path stringByAppendingPathComponent:self.filename]; // // NSError *error = [[NSError alloc] init]; // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; // // NSAssert(bo,@"创建目录失败"); // [self.content writeToFile:filePath atomically:YES]; // // // self.btnshare.enabled =true; // // // self.btnemail.enabled =true; // self.btnsave.enabled =true; // // if(self.onLoadSuccess) // self.onLoadSuccess(self.url); // int a=0; } else { [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self]; // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding]; } ; // NSLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]); // // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL: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 { [self downloadpdfUI]; } - (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 downloadpdf]; //[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