FunctionTestViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. @interface FunctionTestViewController ()
  11. @end
  12. @implementation FunctionTestViewController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. // Do any additional setup after loading the view.
  16. self.airPrinterController = [UIPrintInteractionController sharedPrintController];
  17. }
  18. - (void)viewWillAppear:(BOOL)animated
  19. {
  20. [super viewWillAppear:animated];
  21. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  22. }
  23. - (void)startAirPrintWithData:(id )data
  24. {
  25. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  26. printInfo.outputType = UIPrintInfoOutputGeneral;
  27. printInfo.orientation = UIPrintInfoOrientationPortrait;
  28. printInfo.jobName = @"CoolVisitAirPrint";
  29. self.airPrinterController.printInfo = printInfo;
  30. self.airPrinterController.printingItem = data;
  31. self.airPrinterController.delegate = self;
  32. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  33. if(completed && error)
  34. 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);
  35. };
  36. UIPrinter *airPrinter = [UIPrinter printerWithURL:self.printerURL];///*self.printer;*/
  37. [[UIPrinter printerWithURL:self.printerURL] contactPrinter:^(BOOL available)
  38. {
  39. if (available)
  40. {
  41. [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  42. NSLog(@"AIRPRINTER AVAILABLE");
  43. }
  44. else
  45. {
  46. NSLog(@"AIRPRINTER NOT AVAILABLE");
  47. }
  48. }];
  49. // [airPrinter contactPrinter:^(BOOL available)
  50. // {
  51. // if (available) {
  52. // self.printerdisplayName= airPrinter.displayName;
  53. //// self.printerNameLabel.text = self.savedPrinter.displayName;
  54. //// self.printerLocationLabel.text = self.savedPrinter.displayLocation;
  55. // }
  56. // else {
  57. // NSLog(@"cannot connect to printer");
  58. //// self.printerConnectionGoneIndicator.hidden = NO;
  59. // }
  60. // }];
  61. // [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  62. }
  63. -(void) downloadpdfUI
  64. {
  65. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Loading..."];
  66. NSString* url = @"http://192.168.0.119:8080/site/u/NPD/file/20160315/96ad77c5-e8cd-4bf1-ac97-21c76b56af79.pdf";
  67. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  68. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
  69. //设置请求方式为get
  70. [request setHTTPMethod:@"GET"];
  71. //添加用户会话id
  72. [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  73. //连接发送请求
  74. NSHTTPURLResponse* urlResponse = nil;
  75. NSError *error = nil;
  76. NSData* content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  77. // self.filename = [urlResponse suggestedFilename];
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  80. // self.navigationItem.title = self.save_name;
  81. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  82. // self.content=nil;
  83. if(content!=nil&&content.length>0)
  84. {
  85. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  86. printInfo.outputType = UIPrintInfoOutputGeneral;
  87. printInfo.orientation = UIPrintInfoOrientationPortrait;
  88. printInfo.jobName = @"CoolVisitAirPrint";
  89. self.airPrinterController.printInfo = printInfo;
  90. self.airPrinterController.printingItem = content;
  91. self.airPrinterController.delegate = self;
  92. self.airPrinterController.showsNumberOfCopies=true;
  93. self.airPrinterController.showsPageRange = true;
  94. self.airPrinterController.showsPaperSelectionForLoadedPapers=true;
  95. [self.airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  96. // [printPicker presentAnimated:YES completionHandler:
  97. ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  98. {
  99. if (userDidSelect)
  100. {
  101. UIPrintPaper*paper= printerController.printPaper;
  102. // printerController.printPaper = paper;
  103. // printer
  104. // //User selected the item in the UIPrinterPickerController and got the printer details.
  105. //
  106. // [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  107. //
  108. // //Here you will get the printer and printer details.ie,
  109. // // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  110. //
  111. //
  112. // self.labelPrintername.text =printerPicker.selectedPrinter.displayName;
  113. // // [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
  114. //
  115. //
  116. //
  117. // self.printerurl =printerPicker.selectedPrinter.URL.absoluteString;
  118. // NSURL *printerURL = printerPicker.selectedPrinter.URL;
  119. //
  120. }
  121. }];
  122. //[self startAirPrintWithData:content];
  123. // [reF removeFromSuperview];
  124. //
  125. //
  126. // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  127. // NSString *path = NSTemporaryDirectory();
  128. // NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  129. // // NSError *error = nil;
  130. // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  131. // // NSAssert(bo,@"创建目录失败");
  132. // [self.content writeToFile:filePath atomically:YES];
  133. //
  134. //
  135. // self.btnshare.enabled =true;
  136. //
  137. //
  138. // self.btnemail.enabled =true;
  139. // self.btnsave.enabled =true;
  140. //
  141. // if(self.onLoadSuccess)
  142. // self.onLoadSuccess(self.url);
  143. // int a=0;
  144. }
  145. else
  146. {
  147. [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  148. // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  149. }
  150. ;
  151. // NSLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  152. //
  153. // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  154. });
  155. });
  156. }
  157. -(void) downloadpdf
  158. {
  159. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Loading..."];
  160. NSString* url = @"http://192.168.0.119:8080/site/u/NPD/file/20160315/96ad77c5-e8cd-4bf1-ac97-21c76b56af79.pdf";
  161. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  162. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
  163. //设置请求方式为get
  164. [request setHTTPMethod:@"GET"];
  165. //添加用户会话id
  166. [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  167. //连接发送请求
  168. NSHTTPURLResponse* urlResponse = nil;
  169. NSError *error = nil;
  170. NSData* content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  171. // self.filename = [urlResponse suggestedFilename];
  172. dispatch_async(dispatch_get_main_queue(), ^{
  173. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  174. // self.navigationItem.title = self.save_name;
  175. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  176. // self.content=nil;
  177. if(content!=nil&&content.length>0)
  178. {
  179. [self startAirPrintWithData:content];
  180. // [reF removeFromSuperview];
  181. //
  182. //
  183. // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  184. // NSString *path = NSTemporaryDirectory();
  185. // NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  186. // // NSError *error = nil;
  187. // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  188. // // NSAssert(bo,@"创建目录失败");
  189. // [self.content writeToFile:filePath atomically:YES];
  190. //
  191. //
  192. // self.btnshare.enabled =true;
  193. //
  194. //
  195. // self.btnemail.enabled =true;
  196. // self.btnsave.enabled =true;
  197. //
  198. // if(self.onLoadSuccess)
  199. // self.onLoadSuccess(self.url);
  200. // int a=0;
  201. }
  202. else
  203. {
  204. [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  205. // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  206. }
  207. ;
  208. // NSLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  209. //
  210. // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  211. });
  212. });
  213. }
  214. //-(void)settingPrinter
  215. //{
  216. // UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  217. // CGRect rect;
  218. // UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  219. // rect = CGRectMake(0, 0, 200, 200);
  220. // void (^completionHandler)(UIPrinterPickerController *, BOOL, NSError *) = ^(UIPrinterPickerController *printController, BOOL userDidSelect, NSError * err) {
  221. // if (userDidSelect)
  222. // {
  223. // // save the urlString and Printer name, do your UI interactions
  224. // self.printerurl = pickerController.selectedPrinter.URL.absoluteString;
  225. // self.printerdisplayName = pickerController.selectedPrinter.displayName;
  226. // }
  227. // };
  228. //
  229. //
  230. // [pickerController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
  231. // // [pickerController presentAnimated:YES completionHandler:completionHandler];
  232. //}
  233. - (IBAction)OnPrintParamsClick:(id)sender {
  234. [self downloadpdfUI];
  235. }
  236. - (void) searchForPrinters
  237. {
  238. // For checking the ios version is greater than ios 7.Because skipping the Printing UI is ony in ios8 and later.
  239. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  240. {
  241. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  242. [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  243. // [printPicker presentAnimated:YES completionHandler:
  244. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  245. {
  246. if (userDidSelect)
  247. {
  248. //User selected the item in the UIPrinterPickerController and got the printer details.
  249. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  250. //Here you will get the printer and printer details.ie,
  251. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  252. self.labelPrintername.text =printerPicker.selectedPrinter.displayName;
  253. // [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];
  254. self.printer = printerPicker.selectedPrinter;
  255. self.printerurl =printerPicker.selectedPrinter.URL.absoluteString;
  256. self.printerURL = printerPicker.selectedPrinter.URL;
  257. }
  258. }];
  259. }
  260. }
  261. - (void)didReceiveMemoryWarning {
  262. [super didReceiveMemoryWarning];
  263. // Dispose of any resources that can be recreated.
  264. }
  265. - (IBAction)onBackgroundprintClick:(id)sender {
  266. [self downloadpdf];
  267. //[self startAirPrintWithImage:[UIImage imageNamed:@"notfound_s"]];
  268. }
  269. - (IBAction)onPrintersettingClick:(id)sender {
  270. //[self settingPrinter];
  271. [self searchForPrinters];
  272. }
  273. /*
  274. #pragma mark - Navigation
  275. // In a storyboard-based application, you will often want to do a little preparation before navigation
  276. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  277. // Get the new view controller using [segue destinationViewController].
  278. // Pass the selected object to the new view controller.
  279. }
  280. */
  281. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  282. {
  283. ;
  284. // (int) width = 29700
  285. // Printing description of (*( (int *)0x7caa3a40)):
  286. // (int) height = 42000
  287. //设置纸张大小
  288. for(UIPrintPaper* paper in paperList)
  289. {
  290. if (CGSizeEqualToSize(paper.paperSize, CGSizeMake(612, 792)))
  291. return paper;
  292. }
  293. CGSize paperSize = CGSizeMake(612, 792);
  294. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  295. return p;
  296. }
  297. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  298. {
  299. }
  300. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  301. {
  302. }
  303. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  304. {
  305. }
  306. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  307. {
  308. }
  309. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  310. {
  311. }
  312. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  313. {
  314. }
  315. @end