RAPDFViewController.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. //
  2. // RAPDFViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-8-7.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAPDFViewController.h"
  9. #import "RAUtils.h"
  10. #import "const.h"
  11. #import "AppDelegate.h"
  12. #import "PDFUtils.h"
  13. #import "TextUtils.h"
  14. #import "RANetwork.h"
  15. @interface RAPDFViewController ()
  16. @end
  17. @implementation RAPDFViewController
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. // Custom initialization
  23. }
  24. return self;
  25. }
  26. - (void)setFilename:(NSString *)filename {
  27. _filename = [TextUtils legalFilename:filename];
  28. }
  29. - (void)viewDidLoad
  30. {
  31. NSLog(@"pdf viewer loaded");
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view.
  34. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  35. ref.tag = 200 ;
  36. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
  37. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  38. // ref.hidden = true;
  39. [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  40. [self.wkWebview.scrollView addSubview:ref];
  41. self.wkWebview.navigationDelegate = self;
  42. if (@available(iOS 11, *)) {
  43. self.wkWebview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  44. }
  45. // else {
  46. //
  47. //
  48. //
  49. // self.automaticallyAdjustsScrollViewInsets = NO;
  50. // }
  51. // self.btnsave.tintColor = UIColorFromRGB(0x996633);
  52. if(self.defaultStyle)
  53. {
  54. self.btnshare =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
  55. target:self
  56. action:@selector( onActionClick:)];
  57. self.btnPrint =[[UIBarButtonItem alloc] initWithTitle:@"Print"
  58. style:UIBarButtonItemStylePlain
  59. target:self
  60. action:@selector( onPrintClick:)];
  61. NSMutableArray * items = [[NSMutableArray alloc]init];
  62. [items addObject:self.btnshare];
  63. [items addObject:self.btnPrint];
  64. self.navigationItem.rightBarButtonItems =items;
  65. if (self.navigationController.viewControllers[0]==self )
  66. {
  67. UIBarButtonItem *closeButton =nil;
  68. closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  69. style:UIBarButtonItemStylePlain
  70. target:self
  71. action:@selector( onCloseClick:)];
  72. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  73. // closeButton.title = @"Close";
  74. self.navigationItem.leftBarButtonItem = closeButton;
  75. }
  76. }
  77. else
  78. {
  79. UIBarButtonItem *closeButton =nil;
  80. closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  81. style:UIBarButtonItemStylePlain
  82. target:self
  83. action:@selector( onBackClick:)];
  84. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  85. // closeButton.title = @"Close";
  86. self.navigationItem.leftBarButtonItem = closeButton;
  87. self.btnshare = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"share"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  88. style:UIBarButtonItemStylePlain
  89. target:self
  90. action:@selector(onActionClick:)];//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onOpenOrderClick:)];
  91. // self.btnshare.tintColor = UIColorFromRGB(0x996633);
  92. self.btnemail =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"mail"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  93. style:UIBarButtonItemStylePlain
  94. target:self
  95. action:@selector(onEmailClick:)];// [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Commit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCommitOrderClick:)];
  96. // self.btnemail.tintColor = UIColorFromRGB(0x996633);
  97. // self.btnSend = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Email", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSendMailClick:)];
  98. self.btnsave = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  99. style:UIBarButtonItemStylePlain
  100. target:self
  101. action:@selector(onSaveClick:)];
  102. UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  103. fixedItem.width = 20.0f;
  104. NSMutableArray * items = [[NSMutableArray alloc]init];
  105. [items addObject:self.btnshare];
  106. [items addObject:fixedItem];
  107. [items addObject:self.btnemail];
  108. if(self.canSave)
  109. {
  110. [items addObject:fixedItem];
  111. [items addObject:self.btnsave];
  112. }
  113. self.navigationItem.rightBarButtonItems=items;
  114. self.btnshare.enabled =false;
  115. self.btnemail.enabled =false;
  116. self.btnsave.enabled =false;
  117. }
  118. [self loadcontent];
  119. }
  120. - (void)onBackClick:(UIButton *)sender {
  121. [self.navigationController popViewControllerAnimated:FALSE];
  122. }
  123. - (void)onCloseClick:(UIButton *)sender {
  124. [self dismissViewControllerAnimated:false completion:nil];
  125. //[self.navigationController popViewControllerAnimated:FALSE];
  126. }
  127. -(void)ReloadData
  128. {
  129. // Count ++ ;
  130. // [mytabelview reloadData];
  131. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  132. [reF endRefreshing];
  133. [self loadcontent];
  134. }
  135. - (void)copyTmpLocalFile:(NSString *)localPath {
  136. NSString *path = NSTemporaryDirectory();
  137. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  138. NSFileManager *fileManager = [NSFileManager defaultManager];
  139. if ([fileManager fileExistsAtPath:filePath]) { // 文件存在就不会拷贝
  140. [fileManager removeItemAtPath:filePath error:nil];
  141. }
  142. [fileManager copyItemAtPath:localPath toPath:filePath error:nil];
  143. self.url = filePath;
  144. }
  145. -(void) loadcontent
  146. {
  147. self.mum.hidden=false;
  148. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Loading..." completion:^{
  149. PopWaitAlert* pop =[RAUtils waiting_pop:@"Loading..." completion:nil];
  150. if(self.isLocalfile)
  151. {
  152. /*
  153. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  154. NSString *cachefolder = [paths objectAtIndex:0];
  155. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  156. self.url = [pdfFolder stringByAppendingPathComponent:self.url];
  157. */
  158. self.content= [NSData dataWithContentsOfFile:self.url];
  159. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  160. [pop hide];
  161. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  162. if (self.filename.length <= 0) {
  163. self.filename=[self.url lastPathComponent];
  164. } else {
  165. [self copyTmpLocalFile:self.url];
  166. }
  167. self.btnshare.enabled =true;
  168. self.btnemail.enabled =true;
  169. self.btnsave.enabled =true;
  170. NSURL *url = [NSURL fileURLWithPath:self.url];
  171. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  172. [self.wkWebview loadRequest:request];
  173. }
  174. else
  175. {
  176. [RANetwork request_file_fast:self.url parameters:nil completionHandler:^(NSMutableDictionary *result) {
  177. self.content= result[@"data"];
  178. if(self.filename.length<=0)
  179. self.filename = result[@"suggestedFilename"];
  180. // [waitalert dismissViewControllerAnimated:YES completion:^{
  181. [pop hide];
  182. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  183. self.navigationItem.title = self.save_name;
  184. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  185. // self.content=nil;
  186. if(self.content!=nil&&self.content.length>0)
  187. {
  188. [reF removeFromSuperview];
  189. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  190. NSString *path = NSTemporaryDirectory();
  191. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  192. // NSError *error = nil;
  193. // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  194. // NSAssert(bo,@"创建目录失败");
  195. if (self.cachePath.length) {
  196. filePath = self.cachePath;
  197. }
  198. [self.content writeToFile:filePath atomically:YES];
  199. self.url = filePath;
  200. self.isLocalfile = true;
  201. self.btnshare.enabled =true;
  202. self.btnemail.enabled =true;
  203. self.btnsave.enabled =true;
  204. NSURL *url = [NSURL fileURLWithPath:filePath];
  205. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  206. [self.wkWebview loadRequest:request];
  207. }
  208. else
  209. {
  210. [RAUtils message_box:@"Open PDF Failed" message:nil completion:nil];
  211. if(self.autoPrint)
  212. [self.navigationController popViewControllerAnimated:true];
  213. }
  214. // }];
  215. }];
  216. }
  217. // }];
  218. }
  219. -(void)changeCell
  220. {
  221. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  222. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  223. if ([self respondsToSelector:@selector(ReloadData)])
  224. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  225. // DebugLog(@"refresh!!!!!!!!");
  226. }
  227. - (void)didReceiveMemoryWarning
  228. {
  229. [super didReceiveMemoryWarning];
  230. // Dispose of any resources that can be recreated.
  231. }
  232. -(void) viewWillAppear:(BOOL)animated
  233. {
  234. [super viewWillAppear:animated];
  235. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  236. }
  237. - (IBAction)onPrintClick:(id)sender {
  238. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  239. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  240. printInfo.outputType = UIPrintInfoOutputGeneral;
  241. printInfo.orientation = UIPrintInfoOrientationPortrait;
  242. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  243. if(self.filename.length>0)
  244. printInfo.jobName = self.filename;
  245. airPrinterController.printInfo = printInfo;
  246. airPrinterController.printingItem = self.content;
  247. airPrinterController.delegate = self;
  248. airPrinterController.showsNumberOfCopies=true;
  249. // airPrinterController.showsPageRange = true;
  250. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  251. [airPrinterController presentFromBarButtonItem:self.btnPrint
  252. animated:YES
  253. completionHandler:
  254. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
  255. }
  256. ];
  257. }
  258. - (IBAction)onActionClick:(id)sender {
  259. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  260. // NSString *filePath = nil;
  261. // if(!self.isLocalfile)
  262. // {
  263. // NSString *path = NSTemporaryDirectory();
  264. // NSString* filename =self.filename;
  265. //
  266. // filePath= [path stringByAppendingPathComponent:filename];
  267. //
  268. // }
  269. // else
  270. // filePath = self.url;
  271. [self openFile:self.url];
  272. }
  273. - (IBAction)onSaveClick:(id)sender {
  274. //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
  275. //UIAlertControllerStyleActionSheet类似UIActionSheet
  276. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert];
  277. //block代码块取代了delegate
  278. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  279. textField.text = self.save_name;
  280. }];
  281. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  282. UITextField *name = alertControl.textFields.firstObject;
  283. if(name.text.length==0)
  284. {
  285. [RAUtils message_box:@"Name is missing" message:@"Can not save without name. Save canceled." completion:nil];
  286. }
  287. else
  288. {
  289. self.save_name = name.text;
  290. self.btnsave.enabled =false;
  291. // NSMutableDictionary * info =
  292. if(self.onSavePDF)
  293. self.onSavePDF(self.save_name,self.filename);
  294. }
  295. }];
  296. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  297. DebugLog(@"Cancel");
  298. }];
  299. [alertControl addAction:actionOne];
  300. [alertControl addAction:alertthree];
  301. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  302. [self presentViewController:alertControl animated:YES completion:nil];
  303. }
  304. - (IBAction)onEmailClick:(id)sender {
  305. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  306. // NSString *path = NSTemporaryDirectory();
  307. NSString* filename =self.filename;
  308. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  309. NSString *filePath = self.url;
  310. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  311. if(mc==nil)
  312. {
  313. return;
  314. }
  315. mc.mailComposeDelegate = self;
  316. [mc setSubject:self.mail_subject];
  317. [mc setToRecipients:self.mail_to];
  318. if(self.mail_content==nil)
  319. self.mail_content=@"";
  320. NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",self.mail_content];
  321. [mc setMessageBody:htmlbody
  322. isHTML:YES];
  323. NSData *data = [NSData dataWithContentsOfFile:filePath];
  324. // if(self.attachment_name.length>0)
  325. // filename = self.attachment_name;
  326. [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
  327. // [self presentModalViewController:mc animated:YES];
  328. [self presentViewController:mc animated:YES completion:nil];
  329. return;
  330. }
  331. -(void)openFile: (NSString *) file_url {
  332. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  333. if (file_URL != nil) {
  334. if (self.fileInteractionController == nil) {
  335. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  336. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  337. _fileInteractionController.delegate = self;
  338. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  339. }
  340. else
  341. {
  342. self.fileInteractionController.URL = file_URL;
  343. }
  344. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  345. [self.fileInteractionController setName:self.mail_subject];
  346. // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
  347. [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
  348. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  349. // [self.fileInteractionController presentPreviewAnimated:YES];
  350. }
  351. }
  352. //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
  353. //{
  354. // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
  355. // to, cc, subject, body];
  356. // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  357. //
  358. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
  359. //
  360. //
  361. //}
  362. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  363. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  364. {
  365. // if (controller == self.fileInteractionController) {
  366. // self.fileInteractionController = nil;
  367. // }
  368. }
  369. - (void)previewDocumentWithURL:(NSURL*)url
  370. {
  371. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  372. preview.delegate = self;
  373. [preview presentPreviewAnimated:YES];
  374. }
  375. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  376. {
  377. }
  378. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  379. return self;
  380. }
  381. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  382. return self.view.frame;
  383. }
  384. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  385. return self.view;
  386. }
  387. - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
  388. {
  389. }
  390. #pragma mark - WKNavigationDelegate
  391. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  392. {
  393. self.mum.hidden=true;
  394. if(self.onLoadSuccess)
  395. self.onLoadSuccess(self.url);
  396. if(self.autoPrint)
  397. [self startAirPrintWithData:self.content];
  398. }
  399. #pragma mark -MFMailComposeViewControllerDelegate delegate
  400. - (void)mailComposeController:(MFMailComposeViewController*)controller
  401. didFinishWithResult:(MFMailComposeResult)result
  402. error:(NSError*)error {
  403. switch (result)
  404. {
  405. case MFMailComposeResultCancelled:
  406. DebugLog(@"Mail send canceled...");
  407. break;
  408. case MFMailComposeResultSaved:
  409. DebugLog(@"Mail saved...");
  410. break;
  411. case MFMailComposeResultSent:
  412. DebugLog(@"Mail sent...");
  413. break;
  414. case MFMailComposeResultFailed:
  415. DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
  416. break;
  417. default:
  418. break;
  419. }
  420. // [self dismissModalViewControllerAnimated:YES];
  421. [self dismissViewControllerAnimated:YES completion:nil];
  422. }
  423. - (void)startAirPrintWithData:(id )data
  424. {
  425. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..." completion:^{
  426. PopWaitAlert* pop =[RAUtils waiting_pop:@"Preparing print..." completion:nil];
  427. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  428. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  429. printInfo.outputType = UIPrintInfoOutputGeneral;
  430. printInfo.orientation = UIPrintInfoOrientationPortrait;
  431. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  432. if(self.filename.length>0)
  433. printInfo.jobName = self.filename;
  434. airPrinterController.printInfo = printInfo;
  435. airPrinterController.printingItem = data;
  436. airPrinterController.delegate = self;
  437. airPrinterController.showsNumberOfCopies=true;
  438. // airPrinterController.showsPageRange = true;
  439. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  440. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  441. if(completed && error)
  442. {
  443. // 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);
  444. }
  445. };
  446. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  447. if(appDelegate.printerURL==nil)
  448. {
  449. // [waitalert dismissViewControllerAnimated:YES completion:^{
  450. [pop hide];
  451. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  452. {
  453. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  454. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  455. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  456. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  457. // [printPicker presentAnimated:YES completionHandler:
  458. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  459. {
  460. if (userDidSelect)
  461. {
  462. //User selected the item in the UIPrinterPickerController and got the printer details.
  463. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  464. //Here you will get the printer and printer details.ie,
  465. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  466. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  467. [self startAirPrintWithData:data];
  468. }
  469. else
  470. {
  471. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  472. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  473. // {
  474. //
  475. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  476. // }
  477. // [self prepareReturn:nil];
  478. //[self.navigationController popViewControllerAnimated:true];
  479. }
  480. }];
  481. }
  482. // }];
  483. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  484. }
  485. else
  486. {
  487. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  488. [airPrinter contactPrinter:^(BOOL available)
  489. {
  490. // [waitalert dismissViewControllerAnimated:YES completion:^{
  491. [pop hide];
  492. if (available&& appDelegate.printerURL.length>0)
  493. {
  494. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  495. DebugLog(@"AIRPRINTER AVAILABLE");
  496. }
  497. else
  498. {
  499. appDelegate.printerURL = nil;
  500. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  501. //
  502. //
  503. //
  504. // // [printPicker presentAnimated:YES completionHandler:
  505. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  506. // {
  507. // if (userDidSelect)
  508. // {
  509. // NSString* sid= printerController.printInfo.printerID;
  510. // UIPrintPaper*paper= printerController.printPaper;
  511. //
  512. // }
  513. // }];
  514. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  515. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  516. {
  517. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  518. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  519. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  520. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  521. // [printPicker presentAnimated:YES completionHandler:
  522. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  523. {
  524. if (userDidSelect)
  525. {
  526. //User selected the item in the UIPrinterPickerController and got the printer details.
  527. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  528. //Here you will get the printer and printer details.ie,
  529. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  530. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  531. [self startAirPrintWithData:data];
  532. }
  533. else
  534. {
  535. appDelegate.printerURL = nil;
  536. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  537. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  538. // {
  539. //
  540. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  541. // }
  542. // [self prepareReturn:nil];
  543. //[self.navigationController popViewControllerAnimated:true];
  544. }
  545. }];
  546. }
  547. }
  548. // }];
  549. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  550. }];
  551. }
  552. // }];
  553. }
  554. #pragma mark - UIPrintInteractionControllerDelegate
  555. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  556. {
  557. ;
  558. // (int) width = 29700
  559. // Printing description of (*( (int *)0x7caa3a40)):
  560. // (int) height = 42000
  561. //设置纸张大小
  562. CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
  563. for(UIPrintPaper* paper in paperList)
  564. {
  565. if (CGSizeEqualToSize(paper.paperSize, paperSize))
  566. return paper;
  567. }
  568. // UIPrintPaper * pp=[[UIPrintPaper alloc] init];
  569. // CGSize paperSize = CGSizeMake(612, 792);
  570. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  571. return p;
  572. }
  573. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  574. {
  575. }
  576. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  577. {
  578. }
  579. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  580. {
  581. }
  582. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  583. {
  584. }
  585. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  586. {
  587. }
  588. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  589. {
  590. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  591. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  592. // {
  593. //
  594. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  595. // }
  596. //
  597. // [self prepareReturn:nil];
  598. if(self.autoPrint)
  599. [self.navigationController popViewControllerAnimated:true];
  600. }
  601. @end