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