RAPDFViewController.m 45 KB

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