PDFViewController.m 33 KB

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