FunctionTestViewController.m 17 KB

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