PDFViewController.m 33 KB

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