RAPDFViewController.m 44 KB

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