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. //
  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. return;
  455. }
  456. -(void)openFile: (NSString *) file_url {
  457. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  458. if (file_URL != nil) {
  459. if (self.fileInteractionController == nil) {
  460. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  461. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  462. _fileInteractionController.delegate = self;
  463. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  464. }
  465. else
  466. {
  467. self.fileInteractionController.URL = file_URL;
  468. }
  469. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  470. [self.fileInteractionController setName:self.mail_subject];
  471. // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
  472. [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
  473. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  474. // [self.fileInteractionController presentPreviewAnimated:YES];
  475. }
  476. }
  477. //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
  478. //{
  479. // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
  480. // to, cc, subject, body];
  481. // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  482. //
  483. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
  484. //
  485. //
  486. //}
  487. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  488. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  489. {
  490. // if (controller == self.fileInteractionController) {
  491. // self.fileInteractionController = nil;
  492. // }
  493. }
  494. - (void)previewDocumentWithURL:(NSURL*)url
  495. {
  496. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  497. preview.delegate = self;
  498. [preview presentPreviewAnimated:YES];
  499. }
  500. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  501. {
  502. }
  503. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  504. return self;
  505. }
  506. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  507. return self.view.frame;
  508. }
  509. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  510. return self.view;
  511. }
  512. - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
  513. {
  514. }
  515. #pragma mark - WKNavigationDelegate
  516. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  517. {
  518. self.mum.hidden=true;
  519. if(self.onLoadSuccess)
  520. self.onLoadSuccess(self.url);
  521. if(self.autoPrint)
  522. [self startAirPrintWithData:self.content];
  523. }
  524. #pragma mark -MFMailComposeViewControllerDelegate delegate
  525. - (void)mailComposeController:(MFMailComposeViewController*)controller
  526. didFinishWithResult:(MFMailComposeResult)result
  527. error:(NSError*)error {
  528. switch (result)
  529. {
  530. case MFMailComposeResultCancelled:
  531. DebugLog(@"Mail send canceled...");
  532. break;
  533. case MFMailComposeResultSaved:
  534. DebugLog(@"Mail saved...");
  535. break;
  536. case MFMailComposeResultSent:
  537. DebugLog(@"Mail sent...");
  538. break;
  539. case MFMailComposeResultFailed:
  540. DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
  541. break;
  542. default:
  543. break;
  544. }
  545. [self dismissModalViewControllerAnimated:YES];
  546. }
  547. /*
  548. #pragma mark - Navigation
  549. In a storyboard-based application, you will often want to do a little preparation before navigation
  550. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  551. {
  552. Get the new view controller using [segue destinationViewController].
  553. Pass the selected object to the new view controller.
  554. }
  555. */
  556. //- (void)startAirPrintWithData:(id )data
  557. //{
  558. //
  559. //
  560. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  561. //
  562. // UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  563. // UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  564. // printInfo.outputType = UIPrintInfoOutputGeneral;
  565. // printInfo.orientation = UIPrintInfoOrientationPortrait;
  566. // printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  567. // if(self.filename.length>0)
  568. // printInfo.jobName = self.filename;
  569. // airPrinterController.printInfo = printInfo;
  570. // airPrinterController.printingItem = data;
  571. // airPrinterController.delegate = self;
  572. //
  573. //
  574. // airPrinterController.showsNumberOfCopies=true;
  575. // airPrinterController.showsPageRange = true;
  576. // airPrinterController.showsPaperSelectionForLoadedPapers=true;
  577. //
  578. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  579. // if(completed && error)
  580. // 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);
  581. // };
  582. //
  583. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  584. //
  585. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  586. //
  587. //
  588. //
  589. //
  590. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  591. // {
  592. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  593. //
  594. // if (available&& appDelegate.printerURL.length>0)
  595. // {
  596. //
  597. //
  598. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  599. // DebugLog(@"AIRPRINTER AVAILABLE");
  600. // }
  601. // else
  602. // {
  603. //
  604. // // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  605. // //
  606. // //
  607. // //
  608. // // // [printPicker presentAnimated:YES completionHandler:
  609. // // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  610. // // {
  611. // // if (userDidSelect)
  612. // // {
  613. // // NSString* sid= printerController.printInfo.printerID;
  614. // // UIPrintPaper*paper= printerController.printPaper;
  615. // //
  616. // // }
  617. // // }];
  618. //
  619. //
  620. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  621. //
  622. //
  623. //
  624. // if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  625. // {
  626. // UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  627. // // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  628. //
  629. //
  630. //
  631. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  632. //
  633. //
  634. // // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  635. //
  636. //
  637. //
  638. // // [printPicker presentAnimated:YES completionHandler:
  639. // ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  640. // {
  641. // if (userDidSelect)
  642. // {
  643. // //User selected the item in the UIPrinterPickerController and got the printer details.
  644. //
  645. // [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  646. //
  647. // //Here you will get the printer and printer details.ie,
  648. // // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  649. //
  650. //
  651. //
  652. //
  653. // appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  654. //
  655. // [self startAirPrintWithData:data];
  656. //
  657. // }
  658. // else
  659. // {
  660. //
  661. //
  662. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  663. //// if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  664. //// {
  665. ////
  666. //// [((MainViewController*)appDelegate.main_vc) Loginout:false];
  667. //// }
  668. //// [self prepareReturn:nil];
  669. //
  670. // //[self.navigationController popViewControllerAnimated:true];
  671. // }
  672. // }];
  673. // }
  674. // }
  675. // }];
  676. //
  677. //
  678. //
  679. //}
  680. - (void)startAirPrintWithData:(id )data
  681. {
  682. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  683. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  684. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  685. printInfo.outputType = UIPrintInfoOutputGeneral;
  686. printInfo.orientation = UIPrintInfoOrientationPortrait;
  687. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  688. if(self.filename.length>0)
  689. printInfo.jobName = self.filename;
  690. airPrinterController.printInfo = printInfo;
  691. airPrinterController.printingItem = data;
  692. airPrinterController.delegate = self;
  693. airPrinterController.showsNumberOfCopies=true;
  694. airPrinterController.showsPageRange = true;
  695. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  696. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  697. if(completed && error)
  698. {
  699. // 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);
  700. }
  701. };
  702. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  703. if(appDelegate.printerURL==nil)
  704. {
  705. [waitalert dismissViewControllerAnimated:YES completion:nil];
  706. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  707. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  708. {
  709. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  710. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  711. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  712. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  713. // [printPicker presentAnimated:YES completionHandler:
  714. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  715. {
  716. if (userDidSelect)
  717. {
  718. //User selected the item in the UIPrinterPickerController and got the printer details.
  719. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  720. //Here you will get the printer and printer details.ie,
  721. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  722. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  723. [self startAirPrintWithData:data];
  724. }
  725. else
  726. {
  727. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  728. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  729. // {
  730. //
  731. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  732. // }
  733. // [self prepareReturn:nil];
  734. //[self.navigationController popViewControllerAnimated:true];
  735. }
  736. }];
  737. }
  738. }
  739. else
  740. {
  741. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  742. [airPrinter contactPrinter:^(BOOL available)
  743. {
  744. [waitalert dismissViewControllerAnimated:YES completion:nil];
  745. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  746. if (available&& appDelegate.printerURL.length>0)
  747. {
  748. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  749. DebugLog(@"AIRPRINTER AVAILABLE");
  750. }
  751. else
  752. {
  753. appDelegate.printerURL = nil;
  754. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  755. //
  756. //
  757. //
  758. // // [printPicker presentAnimated:YES completionHandler:
  759. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  760. // {
  761. // if (userDidSelect)
  762. // {
  763. // NSString* sid= printerController.printInfo.printerID;
  764. // UIPrintPaper*paper= printerController.printPaper;
  765. //
  766. // }
  767. // }];
  768. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  769. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  770. {
  771. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  772. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  773. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  774. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  775. // [printPicker presentAnimated:YES completionHandler:
  776. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  777. {
  778. if (userDidSelect)
  779. {
  780. //User selected the item in the UIPrinterPickerController and got the printer details.
  781. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  782. //Here you will get the printer and printer details.ie,
  783. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  784. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  785. [self startAirPrintWithData:data];
  786. }
  787. else
  788. {
  789. appDelegate.printerURL = nil;
  790. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  791. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  792. // {
  793. //
  794. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  795. // }
  796. // [self prepareReturn:nil];
  797. //[self.navigationController popViewControllerAnimated:true];
  798. }
  799. }];
  800. }
  801. }
  802. }];
  803. }
  804. }
  805. #pragma mark - UIPrintInteractionControllerDelegate
  806. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  807. {
  808. ;
  809. // (int) width = 29700
  810. // Printing description of (*( (int *)0x7caa3a40)):
  811. // (int) height = 42000
  812. //设置纸张大小
  813. CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
  814. for(UIPrintPaper* paper in paperList)
  815. {
  816. if (CGSizeEqualToSize(paper.paperSize, paperSize))
  817. return paper;
  818. }
  819. // UIPrintPaper * pp=[[UIPrintPaper alloc] init];
  820. // CGSize paperSize = CGSizeMake(612, 792);
  821. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  822. return p;
  823. }
  824. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  825. {
  826. }
  827. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  828. {
  829. }
  830. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  831. {
  832. }
  833. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  834. {
  835. }
  836. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  837. {
  838. }
  839. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  840. {
  841. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  842. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  843. // {
  844. //
  845. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  846. // }
  847. //
  848. // [self prepareReturn:nil];
  849. if(self.autoPrint)
  850. [self.navigationController popViewControllerAnimated:true];
  851. }
  852. @end