| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- //
- // 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 = nil;
-
- 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 = nil;
- // // 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 = nil;
-
- 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 = nil;
- // // 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<UIPrintPaper *> *)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
|