FunctionTestViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // FunctionTestViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 3/15/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "FunctionTestViewController.h"
  9. #import "RAUtils.h"
  10. #import "AppDelegate.h"
  11. @interface FunctionTestViewController ()
  12. @end
  13. @implementation FunctionTestViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view.
  17. self.airPrinterController = [UIPrintInteractionController sharedPrintController];
  18. }
  19. - (void)viewWillAppear:(BOOL)animated
  20. {
  21. [super viewWillAppear:animated];
  22. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  23. // self.filename = @"print_test";
  24. // NSData* data =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"I25418359" ofType:@"pdf" ]];
  25. // [self startAirPrintWithData1:data];
  26. [self testUI];
  27. }
  28. -(void)testUI
  29. {
  30. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"waiting" completion:^{
  31. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  32. // sleep(2);
  33. // dispatch_async(dispatch_get_main_queue(), ^{
  34. // [waitalert dismissViewControllerAnimated:false completion:^{
  35. //
  36. //
  37. //
  38. // [RAUtils message_box:@"title" message:@"message" completion:nil];
  39. // }];
  40. //
  41. // });
  42. // });
  43. // }];
  44. }
  45. - (void)startAirPrintWithData:(id )data
  46. {
  47. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  48. printInfo.outputType = UIPrintInfoOutputGeneral;
  49. printInfo.orientation = UIPrintInfoOrientationPortrait;
  50. printInfo.jobName = @"CoolVisitAirPrint";
  51. self.airPrinterController.printInfo = printInfo;
  52. self.airPrinterController.printingItem = data;
  53. self.airPrinterController.delegate = self;
  54. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  55. if(completed && error)
  56. 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);
  57. };
  58. UIPrinter *airPrinter = [UIPrinter printerWithURL:self.printerURL];///*self.printer;*/
  59. [[UIPrinter printerWithURL:self.printerURL] contactPrinter:^(BOOL available)
  60. {
  61. if (available)
  62. {
  63. [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  64. NSLog(@"AIRPRINTER AVAILABLE");
  65. }
  66. else
  67. {
  68. NSLog(@"AIRPRINTER NOT AVAILABLE");
  69. }
  70. }];
  71. }
  72. - (void)startAirPrintWithData1:(id )data
  73. {
  74. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..." completion:^{
  75. PopWaitAlert* pop = [RAUtils waiting_pop:@"preparing print..." completion:nil];
  76. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  77. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  78. printInfo.outputType = UIPrintInfoOutputGeneral;
  79. printInfo.orientation = UIPrintInfoOrientationPortrait;
  80. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  81. if(self.filename.length>0)
  82. printInfo.jobName = self.filename;
  83. airPrinterController.printInfo = printInfo;
  84. airPrinterController.printingItem = data;
  85. airPrinterController.delegate = self;
  86. airPrinterController.showsNumberOfCopies=true;
  87. //10.0开始始终显示
  88. // airPrinterController.showsPageRange = true;
  89. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  90. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  91. if(completed && error)
  92. {
  93. // 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);
  94. }
  95. };
  96. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  97. if(appDelegate.printerURL==nil)
  98. {
  99. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  100. [pop hide];
  101. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  102. {
  103. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  104. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  105. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  106. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  107. // [printPicker presentAnimated:YES completionHandler:
  108. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  109. {
  110. if (userDidSelect)
  111. {
  112. //User selected the item in the UIPrinterPickerController and got the printer details.
  113. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  114. //Here you will get the printer and printer details.ie,
  115. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  116. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  117. [self startAirPrintWithData1:data];
  118. }
  119. else
  120. {
  121. }
  122. }];
  123. }
  124. }
  125. else
  126. {
  127. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  128. [airPrinter contactPrinter:^(BOOL available)
  129. {
  130. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  131. [pop hide];
  132. if (available&& appDelegate.printerURL.length>0)
  133. {
  134. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  135. DebugLog(@"AIRPRINTER AVAILABLE");
  136. }
  137. else
  138. {
  139. appDelegate.printerURL = nil;
  140. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  141. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  142. {
  143. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  144. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  145. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  146. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  147. // [printPicker presentAnimated:YES completionHandler:
  148. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  149. {
  150. if (userDidSelect)
  151. {
  152. //User selected the item in the UIPrinterPickerController and got the printer details.
  153. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  154. //Here you will get the printer and printer details.ie,
  155. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  156. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  157. [self startAirPrintWithData:data];
  158. }
  159. else
  160. {
  161. appDelegate.printerURL = nil;
  162. }
  163. }];
  164. }
  165. }
  166. }];
  167. }
  168. // }];
  169. }
  170. //-(void)settingPrinter
  171. //{
  172. // UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  173. // CGRect rect;
  174. // UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  175. // rect = CGRectMake(0, 0, 200, 200);
  176. // void (^completionHandler)(UIPrinterPickerController *, BOOL, NSError *) = ^(UIPrinterPickerController *printController, BOOL userDidSelect, NSError * err) {
  177. // if (userDidSelect)
  178. // {
  179. // // save the urlString and Printer name, do your UI interactions
  180. // self.printerurl = pickerController.selectedPrinter.URL.absoluteString;
  181. // self.printerdisplayName = pickerController.selectedPrinter.displayName;
  182. // }
  183. // };
  184. //
  185. //
  186. // [pickerController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
  187. // // [pickerController presentAnimated:YES completionHandler:completionHandler];
  188. //}
  189. - (IBAction)OnPrintParamsClick:(id)sender {
  190. }
  191. - (void) searchForPrinters
  192. {
  193. // For checking the ios version is greater than ios 7.Because skipping the Printing UI is ony in ios8 and later.
  194. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  195. {
  196. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  197. [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  198. // [printPicker presentAnimated:YES completionHandler:
  199. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  200. {
  201. if (userDidSelect)
  202. {
  203. //User selected the item in the UIPrinterPickerController and got the printer details.
  204. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  205. //Here you will get the printer and printer details.ie,
  206. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  207. self.labelPrintername.text =printerPicker.selectedPrinter.displayName;
  208. // [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
  209. self.printer = printerPicker.selectedPrinter;
  210. self.printerurl =printerPicker.selectedPrinter.URL.absoluteString;
  211. self.printerURL = printerPicker.selectedPrinter.URL;
  212. }
  213. }];
  214. }
  215. }
  216. - (void)didReceiveMemoryWarning {
  217. [super didReceiveMemoryWarning];
  218. // Dispose of any resources that can be recreated.
  219. }
  220. - (IBAction)onBackgroundprintClick:(id)sender {
  221. //[self startAirPrintWithImage:[UIImage imageNamed:@"notfound_s"]];
  222. }
  223. - (IBAction)onPrintersettingClick:(id)sender {
  224. //[self settingPrinter];
  225. [self searchForPrinters];
  226. }
  227. /*
  228. #pragma mark - Navigation
  229. // In a storyboard-based application, you will often want to do a little preparation before navigation
  230. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  231. // Get the new view controller using [segue destinationViewController].
  232. // Pass the selected object to the new view controller.
  233. }
  234. */
  235. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  236. {
  237. ;
  238. // (int) width = 29700
  239. // Printing description of (*( (int *)0x7caa3a40)):
  240. // (int) height = 42000
  241. //设置纸张大小
  242. for(UIPrintPaper* paper in paperList)
  243. {
  244. if (CGSizeEqualToSize(paper.paperSize, CGSizeMake(612, 792)))
  245. return paper;
  246. }
  247. CGSize paperSize = CGSizeMake(612, 792);
  248. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  249. return p;
  250. }
  251. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  252. {
  253. }
  254. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  255. {
  256. }
  257. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  258. {
  259. }
  260. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  261. {
  262. }
  263. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  264. {
  265. }
  266. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  267. {
  268. }
  269. @end