RAPDFViewController.m 50 KB

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