RAPDFViewController.m 44 KB

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