FunctionTestViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." 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. airPrinterController.showsPageRange = true;
  82. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  83. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  84. if(completed && error)
  85. {
  86. // 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);
  87. }
  88. };
  89. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  90. if(appDelegate.printerURL==nil)
  91. {
  92. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  93. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  94. {
  95. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  96. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  97. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  98. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  99. // [printPicker presentAnimated:YES completionHandler:
  100. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  101. {
  102. if (userDidSelect)
  103. {
  104. //User selected the item in the UIPrinterPickerController and got the printer details.
  105. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  106. //Here you will get the printer and printer details.ie,
  107. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  108. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  109. [self startAirPrintWithData1:data];
  110. }
  111. else
  112. {
  113. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  114. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  115. // {
  116. //
  117. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  118. // }
  119. // [self prepareReturn:nil];
  120. //[self.navigationController popViewControllerAnimated:true];
  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 dismissWithClickedButtonIndex:0 animated:FALSE];
  131. if (available&& appDelegate.printerURL.length>0)
  132. {
  133. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  134. DebugLog(@"AIRPRINTER AVAILABLE");
  135. }
  136. else
  137. {
  138. appDelegate.printerURL = nil;
  139. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  140. //
  141. //
  142. //
  143. // // [printPicker presentAnimated:YES completionHandler:
  144. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  145. // {
  146. // if (userDidSelect)
  147. // {
  148. // NSString* sid= printerController.printInfo.printerID;
  149. // UIPrintPaper*paper= printerController.printPaper;
  150. //
  151. // }
  152. // }];
  153. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  154. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  155. {
  156. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  157. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  158. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  159. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  160. // [printPicker presentAnimated:YES completionHandler:
  161. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  162. {
  163. if (userDidSelect)
  164. {
  165. //User selected the item in the UIPrinterPickerController and got the printer details.
  166. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  167. //Here you will get the printer and printer details.ie,
  168. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  169. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  170. [self startAirPrintWithData:data];
  171. }
  172. else
  173. {
  174. appDelegate.printerURL = nil;
  175. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  176. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  177. // {
  178. //
  179. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  180. // }
  181. // [self prepareReturn:nil];
  182. //[self.navigationController popViewControllerAnimated:true];
  183. }
  184. }];
  185. }
  186. }
  187. }];
  188. }
  189. }
  190. //-(void)settingPrinter
  191. //{
  192. // UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  193. // CGRect rect;
  194. // UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  195. // rect = CGRectMake(0, 0, 200, 200);
  196. // void (^completionHandler)(UIPrinterPickerController *, BOOL, NSError *) = ^(UIPrinterPickerController *printController, BOOL userDidSelect, NSError * err) {
  197. // if (userDidSelect)
  198. // {
  199. // // save the urlString and Printer name, do your UI interactions
  200. // self.printerurl = pickerController.selectedPrinter.URL.absoluteString;
  201. // self.printerdisplayName = pickerController.selectedPrinter.displayName;
  202. // }
  203. // };
  204. //
  205. //
  206. // [pickerController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
  207. // // [pickerController presentAnimated:YES completionHandler:completionHandler];
  208. //}
  209. - (IBAction)OnPrintParamsClick:(id)sender {
  210. }
  211. - (void) searchForPrinters
  212. {
  213. // For checking the ios version is greater than ios 7.Because skipping the Printing UI is ony in ios8 and later.
  214. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  215. {
  216. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  217. [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  218. // [printPicker presentAnimated:YES completionHandler:
  219. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  220. {
  221. if (userDidSelect)
  222. {
  223. //User selected the item in the UIPrinterPickerController and got the printer details.
  224. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  225. //Here you will get the printer and printer details.ie,
  226. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  227. self.labelPrintername.text =printerPicker.selectedPrinter.displayName;
  228. // [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
  229. self.printer = printerPicker.selectedPrinter;
  230. self.printerurl =printerPicker.selectedPrinter.URL.absoluteString;
  231. self.printerURL = printerPicker.selectedPrinter.URL;
  232. }
  233. }];
  234. }
  235. }
  236. - (void)didReceiveMemoryWarning {
  237. [super didReceiveMemoryWarning];
  238. // Dispose of any resources that can be recreated.
  239. }
  240. - (IBAction)onBackgroundprintClick:(id)sender {
  241. //[self startAirPrintWithImage:[UIImage imageNamed:@"notfound_s"]];
  242. }
  243. - (IBAction)onPrintersettingClick:(id)sender {
  244. //[self settingPrinter];
  245. [self searchForPrinters];
  246. }
  247. /*
  248. #pragma mark - Navigation
  249. // In a storyboard-based application, you will often want to do a little preparation before navigation
  250. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  251. // Get the new view controller using [segue destinationViewController].
  252. // Pass the selected object to the new view controller.
  253. }
  254. */
  255. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  256. {
  257. ;
  258. // (int) width = 29700
  259. // Printing description of (*( (int *)0x7caa3a40)):
  260. // (int) height = 42000
  261. //设置纸张大小
  262. for(UIPrintPaper* paper in paperList)
  263. {
  264. if (CGSizeEqualToSize(paper.paperSize, CGSizeMake(612, 792)))
  265. return paper;
  266. }
  267. CGSize paperSize = CGSizeMake(612, 792);
  268. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  269. return p;
  270. }
  271. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  272. {
  273. }
  274. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  275. {
  276. }
  277. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  278. {
  279. }
  280. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  281. {
  282. }
  283. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  284. {
  285. }
  286. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  287. {
  288. }
  289. @end