PDFViewController.m 33 KB

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