FunctionTestViewController.m 19 KB

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