PDFViewController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. [self.content writeToFile:filePath atomically:YES];
  185. self.btnshare.enabled =true;
  186. self.btnemail.enabled =true;
  187. self.btnsave.enabled =true;
  188. NSURL *url = [NSURL fileURLWithPath:filePath];
  189. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  190. [self.webView loadRequest:request];
  191. // int a=0;
  192. }
  193. else
  194. {
  195. [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  196. if(self.autoPrint)
  197. [self.navigationController popViewControllerAnimated:true];
  198. // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  199. }
  200. ;
  201. // DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  202. //
  203. // DebugLog(urlResponse.MIMEType);
  204. // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  205. });
  206. });
  207. }
  208. -(void)changeCell
  209. {
  210. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  211. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  212. if ([self respondsToSelector:@selector(ReloadData)])
  213. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  214. // DebugLog(@"refresh!!!!!!!!");
  215. }
  216. - (void)didReceiveMemoryWarning
  217. {
  218. [super didReceiveMemoryWarning];
  219. // Dispose of any resources that can be recreated.
  220. }
  221. -(void) viewWillAppear:(BOOL)animated
  222. {
  223. [super viewWillAppear:animated];
  224. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  225. }
  226. - (IBAction)onPrintClick:(id)sender {
  227. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"preparing print..."];
  228. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  229. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  230. printInfo.outputType = UIPrintInfoOutputGeneral;
  231. printInfo.orientation = UIPrintInfoOrientationPortrait;
  232. printInfo.jobName = @"CoolVisitAirPrint";
  233. airPrinterController.printInfo = printInfo;
  234. airPrinterController.printingItem = self.content;
  235. airPrinterController.delegate = self;
  236. airPrinterController.showsNumberOfCopies=true;
  237. airPrinterController.showsPageRange = true;
  238. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  239. [airPrinterController presentFromBarButtonItem:self.btnPrint
  240. animated:YES
  241. completionHandler:
  242. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
  243. }
  244. ];
  245. //
  246. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  247. // if(completed && error)
  248. // 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);
  249. // };
  250. //
  251. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  252. //
  253. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  254. //
  255. //
  256. //
  257. //
  258. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  259. // {
  260. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  261. //
  262. // if (available&& appDelegate.printerURL.length>0)
  263. // {
  264. //
  265. //
  266. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  267. // DebugLog(@"AIRPRINTER AVAILABLE");
  268. // }
  269. // else
  270. // {
  271. //
  272. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  273. //
  274. //
  275. //
  276. // // [printPicker presentAnimated:YES completionHandler:
  277. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  278. // {
  279. // if (userDidSelect)
  280. // {
  281. // NSString* sid= printerController.printInfo.printerID;
  282. // UIPrintPaper*paper= printerController.printPaper;
  283. //
  284. // }
  285. // }];
  286. //
  287. //
  288. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  289. //
  290. //
  291. //
  292. //// if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  293. //// {
  294. //// UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  295. //// // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  296. ////
  297. ////
  298. ////
  299. //// [ printPicker presentFromBarButtonItem:self.btnPrint animated:YES completionHandler:
  300. ////
  301. ////
  302. //// // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  303. ////
  304. ////
  305. ////
  306. //// // [printPicker presentAnimated:YES completionHandler:
  307. //// ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  308. //// {
  309. //// if (userDidSelect)
  310. //// {
  311. //// //User selected the item in the UIPrinterPickerController and got the printer details.
  312. ////
  313. //// [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  314. ////
  315. //// //Here you will get the printer and printer details.ie,
  316. //// // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  317. ////
  318. ////
  319. ////
  320. ////
  321. //// appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  322. ////
  323. //// [self startAirPrintWithData:self.content];
  324. ////
  325. //// }
  326. //// else
  327. //// {
  328. ////
  329. ////
  330. //// // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  331. //// // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  332. //// // {
  333. //// //
  334. //// // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  335. //// // }
  336. //// // [self prepareReturn:nil];
  337. ////
  338. //// //[self.navigationController popViewControllerAnimated:true];
  339. //// }
  340. //// }];
  341. //// }
  342. // }
  343. // }];
  344. //
  345. }
  346. - (IBAction)onActionClick:(id)sender {
  347. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  348. NSString *filePath = nil;
  349. if(!self.isLocalfile)
  350. {
  351. NSString *path = NSTemporaryDirectory();
  352. NSString* filename =self.filename;
  353. filePath= [path stringByAppendingPathComponent:filename];
  354. }
  355. else
  356. filePath = self.url;
  357. [self openFile:filePath];
  358. }
  359. - (IBAction)onSaveClick:(id)sender {
  360. //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
  361. //UIAlertControllerStyleActionSheet类似UIActionSheet
  362. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert];
  363. //block代码块取代了delegate
  364. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  365. textField.text = self.save_name;
  366. }];
  367. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  368. UITextField *name = alertControl.textFields.firstObject;
  369. if(name.text.length==0)
  370. {
  371. [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
  372. }
  373. else
  374. {
  375. self.save_name = name.text;
  376. self.btnsave.enabled =false;
  377. // NSMutableDictionary * info =
  378. if(self.onSavePDF)
  379. self.onSavePDF(self.save_name,self.filename);
  380. }
  381. }];
  382. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  383. DebugLog(@"Cancel");
  384. }];
  385. [alertControl addAction:actionOne];
  386. [alertControl addAction:alertthree];
  387. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  388. [self presentViewController:alertControl animated:YES completion:nil];
  389. }
  390. - (IBAction)onEmailClick:(id)sender {
  391. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  392. NSString *path = NSTemporaryDirectory();
  393. NSString* filename =self.filename;
  394. NSString *filePath = [path stringByAppendingPathComponent:filename];
  395. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  396. if(mc==nil)
  397. {
  398. //[RAUtils message_alert:@"You must create an email account first." title:@"Send Email" controller:self];
  399. return;
  400. }
  401. mc.mailComposeDelegate = self;
  402. [mc setSubject:self.mail_subject];
  403. [mc setToRecipients:self.mail_to];
  404. if(self.mail_content==nil)
  405. self.mail_content=@"";
  406. NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",self.mail_content];
  407. [mc setMessageBody:htmlbody
  408. isHTML:YES];
  409. NSData *data = [NSData dataWithContentsOfFile:filePath];
  410. if(self.attachment_name.length>0)
  411. filename = self.attachment_name;
  412. [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
  413. [self presentModalViewController:mc animated:YES];
  414. return;
  415. }
  416. -(void)openFile: (NSString *) file_url {
  417. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  418. if (file_URL != nil) {
  419. if (self.fileInteractionController == nil) {
  420. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  421. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  422. _fileInteractionController.delegate = self;
  423. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  424. }
  425. else
  426. {
  427. self.fileInteractionController.URL = file_URL;
  428. }
  429. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  430. [self.fileInteractionController setName:self.mail_subject];
  431. // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
  432. [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
  433. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  434. // [self.fileInteractionController presentPreviewAnimated:YES];
  435. }
  436. }
  437. //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
  438. //{
  439. // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
  440. // to, cc, subject, body];
  441. // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  442. //
  443. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
  444. //
  445. //
  446. //}
  447. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  448. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  449. {
  450. // if (controller == self.fileInteractionController) {
  451. // self.fileInteractionController = nil;
  452. // }
  453. }
  454. - (void)previewDocumentWithURL:(NSURL*)url
  455. {
  456. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  457. preview.delegate = self;
  458. [preview presentPreviewAnimated:YES];
  459. }
  460. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  461. {
  462. }
  463. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  464. return self;
  465. }
  466. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  467. return self.view.frame;
  468. }
  469. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  470. return self.view;
  471. }
  472. - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
  473. {
  474. }
  475. #pragma mark - web view delegate
  476. - (void)webViewDidFinishLoad:(UIWebView *)webView
  477. {
  478. self.mum.hidden=true;
  479. if(self.onLoadSuccess)
  480. self.onLoadSuccess(self.url);
  481. if(self.autoPrint)
  482. [self startAirPrintWithData:self.content];
  483. }
  484. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  485. {
  486. }
  487. #pragma mark -MFMailComposeViewControllerDelegate delegate
  488. - (void)mailComposeController:(MFMailComposeViewController*)controller
  489. didFinishWithResult:(MFMailComposeResult)result
  490. error:(NSError*)error {
  491. switch (result)
  492. {
  493. case MFMailComposeResultCancelled:
  494. DebugLog(@"Mail send canceled...");
  495. break;
  496. case MFMailComposeResultSaved:
  497. DebugLog(@"Mail saved...");
  498. break;
  499. case MFMailComposeResultSent:
  500. DebugLog(@"Mail sent...");
  501. break;
  502. case MFMailComposeResultFailed:
  503. DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
  504. break;
  505. default:
  506. break;
  507. }
  508. [self dismissModalViewControllerAnimated:YES];
  509. }
  510. /*
  511. #pragma mark - Navigation
  512. In a storyboard-based application, you will often want to do a little preparation before navigation
  513. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  514. {
  515. Get the new view controller using [segue destinationViewController].
  516. Pass the selected object to the new view controller.
  517. }
  518. */
  519. - (void)startAirPrintWithData:(id )data
  520. {
  521. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"preparing print..."];
  522. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  523. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  524. printInfo.outputType = UIPrintInfoOutputGeneral;
  525. printInfo.orientation = UIPrintInfoOrientationPortrait;
  526. printInfo.jobName = @"CoolVisitAirPrint";
  527. airPrinterController.printInfo = printInfo;
  528. airPrinterController.printingItem = data;
  529. airPrinterController.delegate = self;
  530. airPrinterController.showsNumberOfCopies=true;
  531. airPrinterController.showsPageRange = true;
  532. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  533. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  534. if(completed && error)
  535. 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);
  536. };
  537. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  538. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  539. [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  540. {
  541. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  542. if (available&& appDelegate.printerURL.length>0)
  543. {
  544. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  545. DebugLog(@"AIRPRINTER AVAILABLE");
  546. }
  547. else
  548. {
  549. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  550. //
  551. //
  552. //
  553. // // [printPicker presentAnimated:YES completionHandler:
  554. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  555. // {
  556. // if (userDidSelect)
  557. // {
  558. // NSString* sid= printerController.printInfo.printerID;
  559. // UIPrintPaper*paper= printerController.printPaper;
  560. //
  561. // }
  562. // }];
  563. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  564. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  565. {
  566. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  567. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  568. [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  569. // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  570. // [printPicker presentAnimated:YES completionHandler:
  571. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  572. {
  573. if (userDidSelect)
  574. {
  575. //User selected the item in the UIPrinterPickerController and got the printer details.
  576. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  577. //Here you will get the printer and printer details.ie,
  578. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  579. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  580. [self startAirPrintWithData:data];
  581. }
  582. else
  583. {
  584. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  585. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  586. // {
  587. //
  588. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  589. // }
  590. // [self prepareReturn:nil];
  591. //[self.navigationController popViewControllerAnimated:true];
  592. }
  593. }];
  594. }
  595. }
  596. }];
  597. }
  598. #pragma mark - UIPrintInteractionControllerDelegate
  599. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  600. {
  601. ;
  602. // (int) width = 29700
  603. // Printing description of (*( (int *)0x7caa3a40)):
  604. // (int) height = 42000
  605. //设置纸张大小
  606. CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
  607. for(UIPrintPaper* paper in paperList)
  608. {
  609. if (CGSizeEqualToSize(paper.paperSize, paperSize))
  610. return paper;
  611. }
  612. UIPrintPaper * pp=[[UIPrintPaper alloc] init];
  613. // CGSize paperSize = CGSizeMake(612, 792);
  614. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  615. return p;
  616. }
  617. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  618. {
  619. }
  620. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  621. {
  622. }
  623. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  624. {
  625. }
  626. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  627. {
  628. }
  629. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  630. {
  631. }
  632. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  633. {
  634. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  635. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  636. // {
  637. //
  638. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  639. // }
  640. //
  641. // [self prepareReturn:nil];
  642. if(self.autoPrint)
  643. [self.navigationController popViewControllerAnimated:true];
  644. }
  645. @end