RAPDFViewController.m 44 KB

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