RAPDFViewController.m 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. @interface RAPDFViewController ()
  15. @end
  16. @implementation RAPDFViewController
  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  18. {
  19. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  20. if (self) {
  21. // Custom initialization
  22. }
  23. return self;
  24. }
  25. - (void)setFilename:(NSString *)filename {
  26. _filename = [TextUtils legalFilename:filename];
  27. }
  28. - (void)viewDidLoad
  29. {
  30. NSLog(@"pdf viewer loaded");
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  34. ref.tag = 200 ;
  35. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
  36. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  37. // ref.hidden = true;
  38. [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  39. [self.wkWebview.scrollView addSubview:ref];
  40. // self.webView.dataDetectorTypes=UIDataDetectorTypeAll;
  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.url=@"http://192.168.1.3:8080/npd/test.pdf";
  119. // NSURL* url = [NSURL URLWithString:self.url];
  120. //
  121. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2000];
  122. //
  123. //
  124. // [self.webView loadRequest:request];
  125. [self loadcontent];
  126. }
  127. - (void)onBackClick:(UIButton *)sender {
  128. [self.navigationController popViewControllerAnimated:FALSE];
  129. }
  130. - (void)onCloseClick:(UIButton *)sender {
  131. [self dismissViewControllerAnimated:false completion:nil];
  132. //[self.navigationController popViewControllerAnimated:FALSE];
  133. }
  134. -(void)ReloadData
  135. {
  136. // Count ++ ;
  137. // [mytabelview reloadData];
  138. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  139. [reF endRefreshing];
  140. [self loadcontent];
  141. }
  142. - (void)copyTmpLocalFile:(NSString *)localPath {
  143. NSString *path = NSTemporaryDirectory();
  144. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  145. NSFileManager *fileManager = [NSFileManager defaultManager];
  146. if ([fileManager fileExistsAtPath:filePath]) { // 文件存在就不会拷贝
  147. [fileManager removeItemAtPath:filePath error:nil];
  148. }
  149. [fileManager copyItemAtPath:localPath toPath:filePath error:nil];
  150. self.url = filePath;
  151. }
  152. -(void) loadcontent
  153. {
  154. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Loading..."];
  155. self.mum.hidden=false;
  156. if(self.isLocalfile)
  157. {
  158. /*
  159. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  160. NSString *cachefolder = [paths objectAtIndex:0];
  161. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  162. self.url = [pdfFolder stringByAppendingPathComponent:self.url];
  163. */
  164. self.content= [NSData dataWithContentsOfFile:self.url];
  165. [waitalert dismissViewControllerAnimated:YES completion:nil];
  166. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  167. if (self.filename.length <= 0) {
  168. self.filename=[self.url lastPathComponent];
  169. } else {
  170. [self copyTmpLocalFile:self.url];
  171. }
  172. self.btnshare.enabled =true;
  173. self.btnemail.enabled =true;
  174. self.btnsave.enabled =true;
  175. // if(self.onLoadSuccess)
  176. // self.onLoadSuccess(self.url);
  177. //
  178. // if(self.autoPrint)
  179. // [self startAirPrintWithData:self.content];
  180. // NSLog(@"put file to webview");
  181. // [self.webView loadData:self.content MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
  182. NSURL *url = [NSURL fileURLWithPath:self.url];
  183. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  184. [self.wkWebview loadRequest:request];
  185. }
  186. else
  187. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  188. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
  189. //设置请求方式为get
  190. [request setHTTPMethod:@"GET"];
  191. //添加用户会话id
  192. [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  193. //连接发送请求
  194. NSHTTPURLResponse* urlResponse = nil;
  195. NSError *error = nil;
  196. self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  197. if(self.filename.length<=0)
  198. self.filename = [urlResponse suggestedFilename];
  199. dispatch_async(dispatch_get_main_queue(), ^{
  200. [waitalert dismissViewControllerAnimated:YES completion:nil];
  201. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  202. self.navigationItem.title = self.save_name;
  203. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  204. // self.content=nil;
  205. if(self.content!=nil&&self.content.length>0)
  206. {
  207. [reF removeFromSuperview];
  208. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  209. NSString *path = NSTemporaryDirectory();
  210. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  211. // NSError *error = nil;
  212. // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  213. // NSAssert(bo,@"创建目录失败");
  214. if (self.cachePath.length) {
  215. filePath = self.cachePath;
  216. }
  217. [self.content writeToFile:filePath atomically:YES];
  218. self.url = filePath;
  219. self.isLocalfile = true;
  220. self.btnshare.enabled =true;
  221. self.btnemail.enabled =true;
  222. self.btnsave.enabled =true;
  223. NSURL *url = [NSURL fileURLWithPath:filePath];
  224. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  225. [self.wkWebview loadRequest:request];
  226. // int a=0;
  227. }
  228. else
  229. {
  230. [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  231. if(self.autoPrint)
  232. [self.navigationController popViewControllerAnimated:true];
  233. // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  234. }
  235. ;
  236. // DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  237. //
  238. // DebugLog(urlResponse.MIMEType);
  239. // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  240. });
  241. });
  242. }
  243. -(void)changeCell
  244. {
  245. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  246. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  247. if ([self respondsToSelector:@selector(ReloadData)])
  248. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  249. // DebugLog(@"refresh!!!!!!!!");
  250. }
  251. - (void)didReceiveMemoryWarning
  252. {
  253. [super didReceiveMemoryWarning];
  254. // Dispose of any resources that can be recreated.
  255. }
  256. -(void) viewWillAppear:(BOOL)animated
  257. {
  258. [super viewWillAppear:animated];
  259. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  260. }
  261. - (IBAction)onPrintClick:(id)sender {
  262. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  263. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  264. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  265. printInfo.outputType = UIPrintInfoOutputGeneral;
  266. printInfo.orientation = UIPrintInfoOrientationPortrait;
  267. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  268. if(self.filename.length>0)
  269. printInfo.jobName = self.filename;
  270. airPrinterController.printInfo = printInfo;
  271. airPrinterController.printingItem = self.content;
  272. airPrinterController.delegate = self;
  273. airPrinterController.showsNumberOfCopies=true;
  274. airPrinterController.showsPageRange = true;
  275. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  276. [airPrinterController presentFromBarButtonItem:self.btnPrint
  277. animated:YES
  278. completionHandler:
  279. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
  280. }
  281. ];
  282. //
  283. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  284. // if(completed && error)
  285. // 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);
  286. // };
  287. //
  288. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  289. //
  290. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  291. //
  292. //
  293. //
  294. //
  295. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  296. // {
  297. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  298. //
  299. // if (available&& appDelegate.printerURL.length>0)
  300. // {
  301. //
  302. //
  303. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  304. // DebugLog(@"AIRPRINTER AVAILABLE");
  305. // }
  306. // else
  307. // {
  308. //
  309. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  310. //
  311. //
  312. //
  313. // // [printPicker presentAnimated:YES completionHandler:
  314. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  315. // {
  316. // if (userDidSelect)
  317. // {
  318. // NSString* sid= printerController.printInfo.printerID;
  319. // UIPrintPaper*paper= printerController.printPaper;
  320. //
  321. // }
  322. // }];
  323. //
  324. //
  325. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  326. //
  327. //
  328. //
  329. //// if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  330. //// {
  331. //// UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  332. //// // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  333. ////
  334. ////
  335. ////
  336. //// [ printPicker presentFromBarButtonItem:self.btnPrint animated:YES completionHandler:
  337. ////
  338. ////
  339. //// // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  340. ////
  341. ////
  342. ////
  343. //// // [printPicker presentAnimated:YES completionHandler:
  344. //// ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  345. //// {
  346. //// if (userDidSelect)
  347. //// {
  348. //// //User selected the item in the UIPrinterPickerController and got the printer details.
  349. ////
  350. //// [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  351. ////
  352. //// //Here you will get the printer and printer details.ie,
  353. //// // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  354. ////
  355. ////
  356. ////
  357. ////
  358. //// appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  359. ////
  360. //// [self startAirPrintWithData:self.content];
  361. ////
  362. //// }
  363. //// else
  364. //// {
  365. ////
  366. ////
  367. //// // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  368. //// // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  369. //// // {
  370. //// //
  371. //// // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  372. //// // }
  373. //// // [self prepareReturn:nil];
  374. ////
  375. //// //[self.navigationController popViewControllerAnimated:true];
  376. //// }
  377. //// }];
  378. //// }
  379. // }
  380. // }];
  381. //
  382. }
  383. - (IBAction)onActionClick:(id)sender {
  384. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  385. // NSString *filePath = nil;
  386. // if(!self.isLocalfile)
  387. // {
  388. // NSString *path = NSTemporaryDirectory();
  389. // NSString* filename =self.filename;
  390. //
  391. // filePath= [path stringByAppendingPathComponent:filename];
  392. //
  393. // }
  394. // else
  395. // filePath = self.url;
  396. [self openFile:self.url];
  397. }
  398. - (IBAction)onSaveClick:(id)sender {
  399. //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
  400. //UIAlertControllerStyleActionSheet类似UIActionSheet
  401. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert];
  402. //block代码块取代了delegate
  403. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  404. textField.text = self.save_name;
  405. }];
  406. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  407. UITextField *name = alertControl.textFields.firstObject;
  408. if(name.text.length==0)
  409. {
  410. [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
  411. }
  412. else
  413. {
  414. self.save_name = name.text;
  415. self.btnsave.enabled =false;
  416. // NSMutableDictionary * info =
  417. if(self.onSavePDF)
  418. self.onSavePDF(self.save_name,self.filename);
  419. }
  420. }];
  421. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  422. DebugLog(@"Cancel");
  423. }];
  424. [alertControl addAction:actionOne];
  425. [alertControl addAction:alertthree];
  426. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  427. [self presentViewController:alertControl animated:YES completion:nil];
  428. }
  429. - (IBAction)onEmailClick:(id)sender {
  430. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  431. // NSString *path = NSTemporaryDirectory();
  432. NSString* filename =self.filename;
  433. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  434. NSString *filePath = self.url;
  435. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  436. if(mc==nil)
  437. {
  438. //[RAUtils message_alert:@"You must create an email account first." title:@"Send Email" controller:self];
  439. return;
  440. }
  441. mc.mailComposeDelegate = self;
  442. [mc setSubject:self.mail_subject];
  443. [mc setToRecipients:self.mail_to];
  444. if(self.mail_content==nil)
  445. self.mail_content=@"";
  446. NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",self.mail_content];
  447. [mc setMessageBody:htmlbody
  448. isHTML:YES];
  449. NSData *data = [NSData dataWithContentsOfFile:filePath];
  450. // if(self.attachment_name.length>0)
  451. // filename = self.attachment_name;
  452. [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
  453. // [self presentModalViewController:mc animated:YES];
  454. [self presentViewController:mc animated:YES completion:nil];
  455. return;
  456. }
  457. -(void)openFile: (NSString *) file_url {
  458. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  459. if (file_URL != nil) {
  460. if (self.fileInteractionController == nil) {
  461. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  462. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  463. _fileInteractionController.delegate = self;
  464. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  465. }
  466. else
  467. {
  468. self.fileInteractionController.URL = file_URL;
  469. }
  470. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  471. [self.fileInteractionController setName:self.mail_subject];
  472. // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
  473. [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
  474. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  475. // [self.fileInteractionController presentPreviewAnimated:YES];
  476. }
  477. }
  478. //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
  479. //{
  480. // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
  481. // to, cc, subject, body];
  482. // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  483. //
  484. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
  485. //
  486. //
  487. //}
  488. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  489. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  490. {
  491. // if (controller == self.fileInteractionController) {
  492. // self.fileInteractionController = nil;
  493. // }
  494. }
  495. - (void)previewDocumentWithURL:(NSURL*)url
  496. {
  497. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  498. preview.delegate = self;
  499. [preview presentPreviewAnimated:YES];
  500. }
  501. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  502. {
  503. }
  504. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  505. return self;
  506. }
  507. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  508. return self.view.frame;
  509. }
  510. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  511. return self.view;
  512. }
  513. - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
  514. {
  515. }
  516. #pragma mark - WKNavigationDelegate
  517. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  518. {
  519. self.mum.hidden=true;
  520. if(self.onLoadSuccess)
  521. self.onLoadSuccess(self.url);
  522. if(self.autoPrint)
  523. [self startAirPrintWithData:self.content];
  524. }
  525. #pragma mark -MFMailComposeViewControllerDelegate delegate
  526. - (void)mailComposeController:(MFMailComposeViewController*)controller
  527. didFinishWithResult:(MFMailComposeResult)result
  528. error:(NSError*)error {
  529. switch (result)
  530. {
  531. case MFMailComposeResultCancelled:
  532. DebugLog(@"Mail send canceled...");
  533. break;
  534. case MFMailComposeResultSaved:
  535. DebugLog(@"Mail saved...");
  536. break;
  537. case MFMailComposeResultSent:
  538. DebugLog(@"Mail sent...");
  539. break;
  540. case MFMailComposeResultFailed:
  541. DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
  542. break;
  543. default:
  544. break;
  545. }
  546. // [self dismissModalViewControllerAnimated:YES];
  547. [self dismissViewControllerAnimated:YES completion:nil];
  548. }
  549. /*
  550. #pragma mark - Navigation
  551. In a storyboard-based application, you will often want to do a little preparation before navigation
  552. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  553. {
  554. Get the new view controller using [segue destinationViewController].
  555. Pass the selected object to the new view controller.
  556. }
  557. */
  558. //- (void)startAirPrintWithData:(id )data
  559. //{
  560. //
  561. //
  562. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  563. //
  564. // UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  565. // UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  566. // printInfo.outputType = UIPrintInfoOutputGeneral;
  567. // printInfo.orientation = UIPrintInfoOrientationPortrait;
  568. // printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  569. // if(self.filename.length>0)
  570. // printInfo.jobName = self.filename;
  571. // airPrinterController.printInfo = printInfo;
  572. // airPrinterController.printingItem = data;
  573. // airPrinterController.delegate = self;
  574. //
  575. //
  576. // airPrinterController.showsNumberOfCopies=true;
  577. // airPrinterController.showsPageRange = true;
  578. // airPrinterController.showsPaperSelectionForLoadedPapers=true;
  579. //
  580. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  581. // if(completed && error)
  582. // 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);
  583. // };
  584. //
  585. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  586. //
  587. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  588. //
  589. //
  590. //
  591. //
  592. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  593. // {
  594. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  595. //
  596. // if (available&& appDelegate.printerURL.length>0)
  597. // {
  598. //
  599. //
  600. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  601. // DebugLog(@"AIRPRINTER AVAILABLE");
  602. // }
  603. // else
  604. // {
  605. //
  606. // // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  607. // //
  608. // //
  609. // //
  610. // // // [printPicker presentAnimated:YES completionHandler:
  611. // // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  612. // // {
  613. // // if (userDidSelect)
  614. // // {
  615. // // NSString* sid= printerController.printInfo.printerID;
  616. // // UIPrintPaper*paper= printerController.printPaper;
  617. // //
  618. // // }
  619. // // }];
  620. //
  621. //
  622. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  623. //
  624. //
  625. //
  626. // if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  627. // {
  628. // UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  629. // // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  630. //
  631. //
  632. //
  633. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  634. //
  635. //
  636. // // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  637. //
  638. //
  639. //
  640. // // [printPicker presentAnimated:YES completionHandler:
  641. // ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  642. // {
  643. // if (userDidSelect)
  644. // {
  645. // //User selected the item in the UIPrinterPickerController and got the printer details.
  646. //
  647. // [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  648. //
  649. // //Here you will get the printer and printer details.ie,
  650. // // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  651. //
  652. //
  653. //
  654. //
  655. // appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  656. //
  657. // [self startAirPrintWithData:data];
  658. //
  659. // }
  660. // else
  661. // {
  662. //
  663. //
  664. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  665. //// if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  666. //// {
  667. ////
  668. //// [((MainViewController*)appDelegate.main_vc) Loginout:false];
  669. //// }
  670. //// [self prepareReturn:nil];
  671. //
  672. // //[self.navigationController popViewControllerAnimated:true];
  673. // }
  674. // }];
  675. // }
  676. // }
  677. // }];
  678. //
  679. //
  680. //
  681. //}
  682. - (void)startAirPrintWithData:(id )data
  683. {
  684. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  685. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  686. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  687. printInfo.outputType = UIPrintInfoOutputGeneral;
  688. printInfo.orientation = UIPrintInfoOrientationPortrait;
  689. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  690. if(self.filename.length>0)
  691. printInfo.jobName = self.filename;
  692. airPrinterController.printInfo = printInfo;
  693. airPrinterController.printingItem = data;
  694. airPrinterController.delegate = self;
  695. airPrinterController.showsNumberOfCopies=true;
  696. airPrinterController.showsPageRange = true;
  697. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  698. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  699. if(completed && error)
  700. {
  701. // 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);
  702. }
  703. };
  704. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  705. if(appDelegate.printerURL==nil)
  706. {
  707. [waitalert dismissViewControllerAnimated:YES completion:nil];
  708. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  709. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  710. {
  711. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  712. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  713. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  714. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  715. // [printPicker presentAnimated:YES completionHandler:
  716. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  717. {
  718. if (userDidSelect)
  719. {
  720. //User selected the item in the UIPrinterPickerController and got the printer details.
  721. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  722. //Here you will get the printer and printer details.ie,
  723. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  724. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  725. [self startAirPrintWithData:data];
  726. }
  727. else
  728. {
  729. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  730. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  731. // {
  732. //
  733. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  734. // }
  735. // [self prepareReturn:nil];
  736. //[self.navigationController popViewControllerAnimated:true];
  737. }
  738. }];
  739. }
  740. }
  741. else
  742. {
  743. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  744. [airPrinter contactPrinter:^(BOOL available)
  745. {
  746. [waitalert dismissViewControllerAnimated:YES completion:nil];
  747. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  748. if (available&& appDelegate.printerURL.length>0)
  749. {
  750. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  751. DebugLog(@"AIRPRINTER AVAILABLE");
  752. }
  753. else
  754. {
  755. appDelegate.printerURL = nil;
  756. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  757. //
  758. //
  759. //
  760. // // [printPicker presentAnimated:YES completionHandler:
  761. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  762. // {
  763. // if (userDidSelect)
  764. // {
  765. // NSString* sid= printerController.printInfo.printerID;
  766. // UIPrintPaper*paper= printerController.printPaper;
  767. //
  768. // }
  769. // }];
  770. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  771. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  772. {
  773. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  774. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  775. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  776. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  777. // [printPicker presentAnimated:YES completionHandler:
  778. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  779. {
  780. if (userDidSelect)
  781. {
  782. //User selected the item in the UIPrinterPickerController and got the printer details.
  783. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  784. //Here you will get the printer and printer details.ie,
  785. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  786. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  787. [self startAirPrintWithData:data];
  788. }
  789. else
  790. {
  791. appDelegate.printerURL = nil;
  792. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  793. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  794. // {
  795. //
  796. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  797. // }
  798. // [self prepareReturn:nil];
  799. //[self.navigationController popViewControllerAnimated:true];
  800. }
  801. }];
  802. }
  803. }
  804. }];
  805. }
  806. }
  807. #pragma mark - UIPrintInteractionControllerDelegate
  808. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  809. {
  810. ;
  811. // (int) width = 29700
  812. // Printing description of (*( (int *)0x7caa3a40)):
  813. // (int) height = 42000
  814. //设置纸张大小
  815. CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
  816. for(UIPrintPaper* paper in paperList)
  817. {
  818. if (CGSizeEqualToSize(paper.paperSize, paperSize))
  819. return paper;
  820. }
  821. // UIPrintPaper * pp=[[UIPrintPaper alloc] init];
  822. // CGSize paperSize = CGSizeMake(612, 792);
  823. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  824. return p;
  825. }
  826. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  827. {
  828. }
  829. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  830. {
  831. }
  832. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  833. {
  834. }
  835. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  836. {
  837. }
  838. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  839. {
  840. }
  841. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  842. {
  843. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  844. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  845. // {
  846. //
  847. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  848. // }
  849. //
  850. // [self prepareReturn:nil];
  851. if(self.autoPrint)
  852. [self.navigationController popViewControllerAnimated:true];
  853. }
  854. @end