RAPDFViewController.m 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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