RAPDFViewController.m 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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_file_fast:self.url parameters:nil 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