FunctionTestViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. }
  27. - (void)startAirPrintWithData:(id )data
  28. {
  29. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  30. printInfo.outputType = UIPrintInfoOutputGeneral;
  31. printInfo.orientation = UIPrintInfoOrientationPortrait;
  32. printInfo.jobName = @"CoolVisitAirPrint";
  33. self.airPrinterController.printInfo = printInfo;
  34. self.airPrinterController.printingItem = data;
  35. self.airPrinterController.delegate = self;
  36. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  37. if(completed && error)
  38. 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);
  39. };
  40. UIPrinter *airPrinter = [UIPrinter printerWithURL:self.printerURL];///*self.printer;*/
  41. [[UIPrinter printerWithURL:self.printerURL] contactPrinter:^(BOOL available)
  42. {
  43. if (available)
  44. {
  45. [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  46. NSLog(@"AIRPRINTER AVAILABLE");
  47. }
  48. else
  49. {
  50. NSLog(@"AIRPRINTER NOT AVAILABLE");
  51. }
  52. }];
  53. // [airPrinter contactPrinter:^(BOOL available)
  54. // {
  55. // if (available) {
  56. // self.printerdisplayName= airPrinter.displayName;
  57. //// self.printerNameLabel.text = self.savedPrinter.displayName;
  58. //// self.printerLocationLabel.text = self.savedPrinter.displayLocation;
  59. // }
  60. // else {
  61. // NSLog(@"cannot connect to printer");
  62. //// self.printerConnectionGoneIndicator.hidden = NO;
  63. // }
  64. // }];
  65. // [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  66. }
  67. - (void)startAirPrintWithData1:(id )data
  68. {
  69. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  70. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  71. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  72. printInfo.outputType = UIPrintInfoOutputGeneral;
  73. printInfo.orientation = UIPrintInfoOrientationPortrait;
  74. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  75. if(self.filename.length>0)
  76. printInfo.jobName = self.filename;
  77. airPrinterController.printInfo = printInfo;
  78. airPrinterController.printingItem = data;
  79. airPrinterController.delegate = self;
  80. airPrinterController.showsNumberOfCopies=true;
  81. //10.0开始始终显示
  82. // airPrinterController.showsPageRange = true;
  83. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  84. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  85. if(completed && error)
  86. {
  87. // 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);
  88. }
  89. };
  90. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  91. if(appDelegate.printerURL==nil)
  92. {
  93. [waitalert dismissViewControllerAnimated:YES completion:nil];
  94. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  95. {
  96. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  97. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  98. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  99. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  100. // [printPicker presentAnimated:YES completionHandler:
  101. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  102. {
  103. if (userDidSelect)
  104. {
  105. //User selected the item in the UIPrinterPickerController and got the printer details.
  106. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  107. //Here you will get the printer and printer details.ie,
  108. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  109. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  110. [self startAirPrintWithData1:data];
  111. }
  112. else
  113. {
  114. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  115. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  116. // {
  117. //
  118. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  119. // }
  120. // [self prepareReturn:nil];
  121. //[self.navigationController popViewControllerAnimated:true];
  122. }
  123. }];
  124. }
  125. }
  126. else
  127. {
  128. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  129. [airPrinter contactPrinter:^(BOOL available)
  130. {
  131. [waitalert dismissViewControllerAnimated:YES completion:nil];
  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. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  141. //
  142. //
  143. //
  144. // // [printPicker presentAnimated:YES completionHandler:
  145. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  146. // {
  147. // if (userDidSelect)
  148. // {
  149. // NSString* sid= printerController.printInfo.printerID;
  150. // UIPrintPaper*paper= printerController.printPaper;
  151. //
  152. // }
  153. // }];
  154. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  155. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  156. {
  157. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  158. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  159. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  160. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  161. // [printPicker presentAnimated:YES completionHandler:
  162. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  163. {
  164. if (userDidSelect)
  165. {
  166. //User selected the item in the UIPrinterPickerController and got the printer details.
  167. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  168. //Here you will get the printer and printer details.ie,
  169. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  170. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  171. [self startAirPrintWithData:data];
  172. }
  173. else
  174. {
  175. appDelegate.printerURL = nil;
  176. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  177. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  178. // {
  179. //
  180. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  181. // }
  182. // [self prepareReturn:nil];
  183. //[self.navigationController popViewControllerAnimated:true];
  184. }
  185. }];
  186. }
  187. }
  188. }];
  189. }
  190. }
  191. //-(void)settingPrinter
  192. //{
  193. // UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  194. // CGRect rect;
  195. // UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  196. // rect = CGRectMake(0, 0, 200, 200);
  197. // void (^completionHandler)(UIPrinterPickerController *, BOOL, NSError *) = ^(UIPrinterPickerController *printController, BOOL userDidSelect, NSError * err) {
  198. // if (userDidSelect)
  199. // {
  200. // // save the urlString and Printer name, do your UI interactions
  201. // self.printerurl = pickerController.selectedPrinter.URL.absoluteString;
  202. // self.printerdisplayName = pickerController.selectedPrinter.displayName;
  203. // }
  204. // };
  205. //
  206. //
  207. // [pickerController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
  208. // // [pickerController presentAnimated:YES completionHandler:completionHandler];
  209. //}
  210. - (IBAction)OnPrintParamsClick:(id)sender {
  211. }
  212. - (void) searchForPrinters
  213. {
  214. // For checking the ios version is greater than ios 7.Because skipping the Printing UI is ony in ios8 and later.
  215. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  216. {
  217. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  218. [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  219. // [printPicker presentAnimated:YES completionHandler:
  220. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  221. {
  222. if (userDidSelect)
  223. {
  224. //User selected the item in the UIPrinterPickerController and got the printer details.
  225. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  226. //Here you will get the printer and printer details.ie,
  227. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  228. self.labelPrintername.text =printerPicker.selectedPrinter.displayName;
  229. // [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
  230. self.printer = printerPicker.selectedPrinter;
  231. self.printerurl =printerPicker.selectedPrinter.URL.absoluteString;
  232. self.printerURL = printerPicker.selectedPrinter.URL;
  233. }
  234. }];
  235. }
  236. }
  237. - (void)didReceiveMemoryWarning {
  238. [super didReceiveMemoryWarning];
  239. // Dispose of any resources that can be recreated.
  240. }
  241. - (IBAction)onBackgroundprintClick:(id)sender {
  242. //[self startAirPrintWithImage:[UIImage imageNamed:@"notfound_s"]];
  243. }
  244. - (IBAction)onPrintersettingClick:(id)sender {
  245. //[self settingPrinter];
  246. [self searchForPrinters];
  247. }
  248. /*
  249. #pragma mark - Navigation
  250. // In a storyboard-based application, you will often want to do a little preparation before navigation
  251. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  252. // Get the new view controller using [segue destinationViewController].
  253. // Pass the selected object to the new view controller.
  254. }
  255. */
  256. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  257. {
  258. ;
  259. // (int) width = 29700
  260. // Printing description of (*( (int *)0x7caa3a40)):
  261. // (int) height = 42000
  262. //设置纸张大小
  263. for(UIPrintPaper* paper in paperList)
  264. {
  265. if (CGSizeEqualToSize(paper.paperSize, CGSizeMake(612, 792)))
  266. return paper;
  267. }
  268. CGSize paperSize = CGSizeMake(612, 792);
  269. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  270. return p;
  271. }
  272. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  273. {
  274. }
  275. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  276. {
  277. }
  278. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  279. {
  280. }
  281. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  282. {
  283. }
  284. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  285. {
  286. }
  287. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  288. {
  289. }
  290. @end