| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234 |
- //
- // RAPDFViewController.m
- // RedAnt ERP Mobile
- //
- // Created by Ray on 14-8-7.
- // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
- //
- #import "RAPDFViewController.h"
- #import "RAUtils.h"
- #import "const.h"
- #import "AppDelegate.h"
- #import "PDFUtils.h"
- #import "TextUtils.h"
- #import "RANetwork.h"
- @interface RAPDFViewController ()
- @end
- @implementation RAPDFViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)setFilename:(NSString *)filename {
- _filename = [TextUtils legalFilename:filename];
- }
- - (void)viewDidLoad
- {
-
- NSLog(@"pdf viewer loaded");
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- UIRefreshControl *ref = [[UIRefreshControl alloc]init];
- ref.tag = 200 ;
- ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
- ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
- // ref.hidden = true;
- [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
- [self.wkWebview.scrollView addSubview:ref];
- // self.webView.dataDetectorTypes=UIDataDetectorTypeAll;
- self.wkWebview.navigationDelegate = self;
- if (@available(iOS 11, *)) {
- self.wkWebview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- // else {
- //
- //
- //
- // self.automaticallyAdjustsScrollViewInsets = NO;
- // }
- // self.btnsave.tintColor = UIColorFromRGB(0x996633);
-
- if(self.defaultStyle)
- {
- self.btnshare =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
- target:self
- action:@selector( onActionClick:)];
-
-
- self.btnPrint =[[UIBarButtonItem alloc] initWithTitle:@"Print"
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector( onPrintClick:)];
-
- NSMutableArray * items = [[NSMutableArray alloc]init];
- [items addObject:self.btnshare];
- [items addObject:self.btnPrint];
- self.navigationItem.rightBarButtonItems =items;
-
-
- if (self.navigationController.viewControllers[0]==self )
- {
- UIBarButtonItem *closeButton =nil;
-
-
-
- closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector( onCloseClick:)];
- //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
- // closeButton.title = @"Close";
-
- self.navigationItem.leftBarButtonItem = closeButton;
- }
-
- }
- else
- {
- UIBarButtonItem *closeButton =nil;
-
-
-
- closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector( onBackClick:)];
- //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
- // closeButton.title = @"Close";
-
- self.navigationItem.leftBarButtonItem = closeButton;
-
-
-
-
- self.btnshare = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"share"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(onActionClick:)];//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onOpenOrderClick:)];
-
- // self.btnshare.tintColor = UIColorFromRGB(0x996633);
-
- self.btnemail =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"mail"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(onEmailClick:)];// [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Commit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCommitOrderClick:)];
-
- // self.btnemail.tintColor = UIColorFromRGB(0x996633);
-
- // self.btnSend = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Email", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSendMailClick:)];
-
-
-
- self.btnsave = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(onSaveClick:)];
- UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
- fixedItem.width = 20.0f;
- NSMutableArray * items = [[NSMutableArray alloc]init];
- [items addObject:self.btnshare];
- [items addObject:fixedItem];
- [items addObject:self.btnemail];
-
- if(self.canSave)
- {
- [items addObject:fixedItem];
- [items addObject:self.btnsave];
- }
- self.navigationItem.rightBarButtonItems=items;
-
- self.btnshare.enabled =false;
-
-
- self.btnemail.enabled =false;
- self.btnsave.enabled =false;
-
- }
-
- // self.url=@"http://192.168.1.3:8080/npd/test.pdf";
- // NSURL* url = [NSURL URLWithString:self.url];
- //
- // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2000];
- //
- //
- // [self.webView loadRequest:request];
-
-
- [self loadcontent];
- }
- - (void)onBackClick:(UIButton *)sender {
-
-
- [self.navigationController popViewControllerAnimated:FALSE];
- }
- - (void)onCloseClick:(UIButton *)sender {
-
- [self dismissViewControllerAnimated:false completion:nil];
- //[self.navigationController popViewControllerAnimated:FALSE];
- }
- -(void)ReloadData
- {
-
- // Count ++ ;
- // [mytabelview reloadData];
- UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
-
- [reF endRefreshing];
- [self loadcontent];
-
- }
- - (void)copyTmpLocalFile:(NSString *)localPath {
-
- NSString *path = NSTemporaryDirectory();
- NSString *filePath = [path stringByAppendingPathComponent:self.filename];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:filePath]) { // 文件存在就不会拷贝
- [fileManager removeItemAtPath:filePath error:nil];
- }
- [fileManager copyItemAtPath:localPath toPath:filePath error:nil];
- self.url = filePath;
- }
- -(void) loadcontent
- {
- self.mum.hidden=false;
- __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Loading..." completion:^{
- if(self.isLocalfile)
- {
- /*
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachefolder = [paths objectAtIndex:0];
- NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
- self.url = [pdfFolder stringByAppendingPathComponent:self.url];
- */
-
- self.content= [NSData dataWithContentsOfFile:self.url];
-
- [waitalert dismissViewControllerAnimated:YES completion:nil];
-
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
-
- if (self.filename.length <= 0) {
- self.filename=[self.url lastPathComponent];
- } else {
- [self copyTmpLocalFile:self.url];
- }
-
- self.btnshare.enabled =true;
-
-
- self.btnemail.enabled =true;
- self.btnsave.enabled =true;
-
- // if(self.onLoadSuccess)
- // self.onLoadSuccess(self.url);
- //
- // if(self.autoPrint)
- // [self startAirPrintWithData:self.content];
-
- // NSLog(@"put file to webview");
- // [self.webView loadData:self.content MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
-
- NSURL *url = [NSURL fileURLWithPath:self.url];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- [self.wkWebview loadRequest:request];
-
- }
- else
- {
-
-
-
- [RANetwork request_file_fast:self.url parameters:nil completionHandler:^(NSMutableDictionary *result) {
- self.content= result[@"data"];
- if(self.filename.length<=0)
- self.filename = result[@"suggestedFilename"];
-
-
- [waitalert dismissViewControllerAnimated:YES completion:^{
- [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
- self.navigationItem.title = self.save_name;
- UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
- // self.content=nil;
- if(self.content!=nil&&self.content.length>0)
- {
- [reF removeFromSuperview];
-
-
- // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = NSTemporaryDirectory();
- NSString *filePath = [path stringByAppendingPathComponent:self.filename];
- // NSError *error = nil;
- // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
- // NSAssert(bo,@"创建目录失败");
- if (self.cachePath.length) {
- filePath = self.cachePath;
- }
-
- [self.content writeToFile:filePath atomically:YES];
-
- self.url = filePath;
- self.isLocalfile = true;
-
-
- self.btnshare.enabled =true;
-
-
- self.btnemail.enabled =true;
- self.btnsave.enabled =true;
-
-
-
- NSURL *url = [NSURL fileURLWithPath:filePath];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- [self.wkWebview loadRequest:request];
-
-
- }
- else
- {
- [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
-
- if(self.autoPrint)
- [self.navigationController popViewControllerAnimated:true];
- }
- }];
-
-
-
-
-
-
- }];
-
-
-
- // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- //
- // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
- //
- // //设置请求方式为get
- //
- // [request setHTTPMethod:@"GET"];
- //
- // //添加用户会话id
- //
- // [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
- //
- // //连接发送请求
- //
- //
- // NSHTTPURLResponse* urlResponse = nil;
- //
- // NSError *error = nil;
- //
- // self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
- //
- // if(self.filename.length<=0)
- // self.filename = [urlResponse suggestedFilename];
- //
- //
- // dispatch_async(dispatch_get_main_queue(), ^{
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- //
- // [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
- // self.navigationItem.title = self.save_name;
- // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
- // // self.content=nil;
- // if(self.content!=nil&&self.content.length>0)
- // {
- // [reF removeFromSuperview];
- //
- //
- // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- // NSString *path = NSTemporaryDirectory();
- // NSString *filePath = [path stringByAppendingPathComponent:self.filename];
- // // NSError *error = nil;
- // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
- // // NSAssert(bo,@"创建目录失败");
- // if (self.cachePath.length) {
- // filePath = self.cachePath;
- // }
- //
- // [self.content writeToFile:filePath atomically:YES];
- //
- // self.url = filePath;
- // self.isLocalfile = true;
- //
- //
- // self.btnshare.enabled =true;
- //
- //
- // self.btnemail.enabled =true;
- // self.btnsave.enabled =true;
- //
- //
- //
- // NSURL *url = [NSURL fileURLWithPath:filePath];
- // NSURLRequest *request = [NSURLRequest requestWithURL:url];
- // [self.wkWebview loadRequest:request];
- //
- //
- //
- //
- //
- //
- // // int a=0;
- // }
- // else
- // {
- // [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
- //
- // if(self.autoPrint)
- // [self.navigationController popViewControllerAnimated:true];
- // // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
- // }
- //
- // ;
- // // DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
- // //
- //
- // // DebugLog(urlResponse.MIMEType);
- //
- // // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
- //
- //
- //
- //
- // });
- // });
- }
- }];
-
-
-
-
-
-
-
-
- }
- -(void)changeCell
- {
- UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
- reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
- if ([self respondsToSelector:@selector(ReloadData)])
- [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
- // DebugLog(@"refresh!!!!!!!!");
-
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- -(void) viewWillAppear:(BOOL)animated
- {
-
-
- [super viewWillAppear:animated];
-
- [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
- }
- - (IBAction)onPrintClick:(id)sender {
-
-
- // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
-
- UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
- UIPrintInfo *printInfo = [UIPrintInfo printInfo];
- printInfo.outputType = UIPrintInfoOutputGeneral;
-
- printInfo.orientation = UIPrintInfoOrientationPortrait;
- printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
- if(self.filename.length>0)
- printInfo.jobName = self.filename;
- airPrinterController.printInfo = printInfo;
- airPrinterController.printingItem = self.content;
- airPrinterController.delegate = self;
-
-
- airPrinterController.showsNumberOfCopies=true;
- // airPrinterController.showsPageRange = true;
- airPrinterController.showsPaperSelectionForLoadedPapers=true;
-
-
- [airPrinterController presentFromBarButtonItem:self.btnPrint
- animated:YES
- completionHandler:
- ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
- }
- ];
-
-
- //
- // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
- // if(completed && error)
- // 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);
- // };
- //
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- //
- // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
- //
- //
- //
- //
- // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
- // {
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- //
- // if (available&& appDelegate.printerURL.length>0)
- // {
- //
- //
- // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
- // DebugLog(@"AIRPRINTER AVAILABLE");
- // }
- // else
- // {
- //
- // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
- //
- //
- //
- // // [printPicker presentAnimated:YES completionHandler:
- // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
- // {
- // if (userDidSelect)
- // {
- // NSString* sid= printerController.printInfo.printerID;
- // UIPrintPaper*paper= printerController.printPaper;
- //
- // }
- // }];
- //
- //
- // DebugLog(@"AIRPRINTER NOT AVAILABLE");
- //
- //
- //
- //// if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
- //// {
- //// UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
- //// // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
- ////
- ////
- ////
- //// [ printPicker presentFromBarButtonItem:self.btnPrint animated:YES completionHandler:
- ////
- ////
- //// // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
- ////
- ////
- ////
- //// // [printPicker presentAnimated:YES completionHandler:
- //// ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
- //// {
- //// if (userDidSelect)
- //// {
- //// //User selected the item in the UIPrinterPickerController and got the printer details.
- ////
- //// [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
- ////
- //// //Here you will get the printer and printer details.ie,
- //// // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
- ////
- ////
- ////
- ////
- //// appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
- ////
- //// [self startAirPrintWithData:self.content];
- ////
- //// }
- //// else
- //// {
- ////
- ////
- //// // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- //// // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
- //// // {
- //// //
- //// // [((MainViewController*)appDelegate.main_vc) Loginout:false];
- //// // }
- //// // [self prepareReturn:nil];
- ////
- //// //[self.navigationController popViewControllerAnimated:true];
- //// }
- //// }];
- //// }
- // }
- // }];
- //
-
-
- }
- - (IBAction)onActionClick:(id)sender {
-
- //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
- // NSString *filePath = nil;
- // if(!self.isLocalfile)
- // {
- // NSString *path = NSTemporaryDirectory();
- // NSString* filename =self.filename;
- //
- // filePath= [path stringByAppendingPathComponent:filename];
- //
- // }
- // else
- // filePath = self.url;
-
-
-
-
- [self openFile:self.url];
- }
- - (IBAction)onSaveClick:(id)sender {
-
- //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
- //UIAlertControllerStyleActionSheet类似UIActionSheet
- UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert];
- //block代码块取代了delegate
-
-
- [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
- textField.text = self.save_name;
-
-
- }];
-
- UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
-
- UITextField *name = alertControl.textFields.firstObject;
- if(name.text.length==0)
- {
- [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
- }
- else
- {
- self.save_name = name.text;
- self.btnsave.enabled =false;
- // NSMutableDictionary * info =
- if(self.onSavePDF)
- self.onSavePDF(self.save_name,self.filename);
- }
- }];
- UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- DebugLog(@"Cancel");
- }];
- [alertControl addAction:actionOne];
- [alertControl addAction:alertthree];
-
- //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
-
- [self presentViewController:alertControl animated:YES completion:nil];
-
-
-
-
- }
- - (IBAction)onEmailClick:(id)sender {
- //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- // NSString *path = NSTemporaryDirectory();
- NSString* filename =self.filename;
-
- // NSString *filePath = [path stringByAppendingPathComponent:filename];
- NSString *filePath = self.url;
-
-
-
-
- MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
-
- if(mc==nil)
- {
- //[RAUtils message_alert:@"You must create an email account first." title:@"Send Email" controller:self];
- return;
- }
- mc.mailComposeDelegate = self;
- [mc setSubject:self.mail_subject];
-
-
- [mc setToRecipients:self.mail_to];
-
-
- if(self.mail_content==nil)
- self.mail_content=@"";
-
- NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",self.mail_content];
-
- [mc setMessageBody:htmlbody
- isHTML:YES];
-
-
- NSData *data = [NSData dataWithContentsOfFile:filePath];
-
- // if(self.attachment_name.length>0)
- // filename = self.attachment_name;
- [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
- // [self presentModalViewController:mc animated:YES];
- [self presentViewController:mc animated:YES completion:nil];
-
- return;
- }
- -(void)openFile: (NSString *) file_url {
-
- NSURL *file_URL = [NSURL fileURLWithPath:file_url];
-
- if (file_URL != nil) {
- if (self.fileInteractionController == nil) {
- //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
-
-
- _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
- _fileInteractionController.delegate = self;
- // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
- }
- else
- {
- self.fileInteractionController.URL = file_URL;
- }
-
- // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
-
-
- [self.fileInteractionController setName:self.mail_subject];
-
- // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
-
- [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
- // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
- // [self.fileInteractionController presentPreviewAnimated:YES];
-
- }
- }
- //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
- //{
- // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
- // to, cc, subject, body];
- // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- //
- // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
- //
- //
- //}
- #pragma mark methods for the UIDocumentInteractionControllerDelegate
- - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
- {
- // if (controller == self.fileInteractionController) {
- // self.fileInteractionController = nil;
- // }
- }
- - (void)previewDocumentWithURL:(NSURL*)url
- {
- UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
- preview.delegate = self;
- [preview presentPreviewAnimated:YES];
-
- }
- - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
- {
-
- }
- - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
- return self;
- }
- - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
- return self.view.frame;
- }
- - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
- return self.view;
- }
- - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
- {
-
- }
- #pragma mark - WKNavigationDelegate
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
- {
- self.mum.hidden=true;
- if(self.onLoadSuccess)
- self.onLoadSuccess(self.url);
- if(self.autoPrint)
- [self startAirPrintWithData:self.content];
-
- }
- #pragma mark -MFMailComposeViewControllerDelegate delegate
- - (void)mailComposeController:(MFMailComposeViewController*)controller
- didFinishWithResult:(MFMailComposeResult)result
- error:(NSError*)error {
- switch (result)
- {
- case MFMailComposeResultCancelled:
- DebugLog(@"Mail send canceled...");
- break;
- case MFMailComposeResultSaved:
- DebugLog(@"Mail saved...");
- break;
- case MFMailComposeResultSent:
- DebugLog(@"Mail sent...");
- break;
- case MFMailComposeResultFailed:
- DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
- break;
- default:
- break;
- }
- // [self dismissModalViewControllerAnimated:YES];
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- /*
- #pragma mark - Navigation
- In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- Get the new view controller using [segue destinationViewController].
- Pass the selected object to the new view controller.
- }
- */
- //- (void)startAirPrintWithData:(id )data
- //{
- //
- //
- // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
- //
- // UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
- // UIPrintInfo *printInfo = [UIPrintInfo printInfo];
- // printInfo.outputType = UIPrintInfoOutputGeneral;
- // printInfo.orientation = UIPrintInfoOrientationPortrait;
- // printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
- // if(self.filename.length>0)
- // printInfo.jobName = self.filename;
- // airPrinterController.printInfo = printInfo;
- // airPrinterController.printingItem = data;
- // airPrinterController.delegate = self;
- //
- //
- // airPrinterController.showsNumberOfCopies=true;
- // airPrinterController.showsPageRange = true;
- // airPrinterController.showsPaperSelectionForLoadedPapers=true;
- //
- // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
- // if(completed && error)
- // 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);
- // };
- //
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- //
- // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
- //
- //
- //
- //
- // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
- // {
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- //
- // if (available&& appDelegate.printerURL.length>0)
- // {
- //
- //
- // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
- // DebugLog(@"AIRPRINTER AVAILABLE");
- // }
- // else
- // {
- //
- // // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
- // //
- // //
- // //
- // // // [printPicker presentAnimated:YES completionHandler:
- // // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
- // // {
- // // if (userDidSelect)
- // // {
- // // NSString* sid= printerController.printInfo.printerID;
- // // UIPrintPaper*paper= printerController.printPaper;
- // //
- // // }
- // // }];
- //
- //
- // DebugLog(@"AIRPRINTER NOT AVAILABLE");
- //
- //
- //
- // if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
- // {
- // UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
- // // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
- //
- //
- //
- // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
- //
- //
- // // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
- //
- //
- //
- // // [printPicker presentAnimated:YES completionHandler:
- // ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
- // {
- // if (userDidSelect)
- // {
- // //User selected the item in the UIPrinterPickerController and got the printer details.
- //
- // [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
- //
- // //Here you will get the printer and printer details.ie,
- // // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
- //
- //
- //
- //
- // appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
- //
- // [self startAirPrintWithData:data];
- //
- // }
- // else
- // {
- //
- //
- //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- //// if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
- //// {
- ////
- //// [((MainViewController*)appDelegate.main_vc) Loginout:false];
- //// }
- //// [self prepareReturn:nil];
- //
- // //[self.navigationController popViewControllerAnimated:true];
- // }
- // }];
- // }
- // }
- // }];
- //
- //
- //
- //}
- - (void)startAirPrintWithData:(id )data
- {
-
-
- __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..." completion:^{
-
- UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
- UIPrintInfo *printInfo = [UIPrintInfo printInfo];
- printInfo.outputType = UIPrintInfoOutputGeneral;
- printInfo.orientation = UIPrintInfoOrientationPortrait;
- printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
- if(self.filename.length>0)
- printInfo.jobName = self.filename;
- airPrinterController.printInfo = printInfo;
- airPrinterController.printingItem = data;
- airPrinterController.delegate = self;
-
-
- airPrinterController.showsNumberOfCopies=true;
- // airPrinterController.showsPageRange = true;
- airPrinterController.showsPaperSelectionForLoadedPapers=true;
-
- void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
- if(completed && error)
- {
- // 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);
- }
-
- };
-
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
-
- if(appDelegate.printerURL==nil)
- {
- [waitalert dismissViewControllerAnimated:YES completion:^{
- if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
- {
- UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
- // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
-
-
-
- // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
-
-
-
- [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
-
-
-
- // [printPicker presentAnimated:YES completionHandler:
- ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
- {
- if (userDidSelect)
- {
- //User selected the item in the UIPrinterPickerController and got the printer details.
-
- [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
-
- //Here you will get the printer and printer details.ie,
- // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
-
-
-
-
- appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
-
- [self startAirPrintWithData:data];
-
- }
- else
- {
-
-
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
- // {
- //
- // [((MainViewController*)appDelegate.main_vc) Loginout:false];
- // }
- // [self prepareReturn:nil];
-
- //[self.navigationController popViewControllerAnimated:true];
- }
- }];
- }
- }];
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
- }
- else
- {
- UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
- [airPrinter contactPrinter:^(BOOL available)
- {
-
- [waitalert dismissViewControllerAnimated:YES completion:^{
- if (available&& appDelegate.printerURL.length>0)
- {
-
-
- [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
- DebugLog(@"AIRPRINTER AVAILABLE");
- }
- else
- {
- appDelegate.printerURL = nil;
-
- // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
- //
- //
- //
- // // [printPicker presentAnimated:YES completionHandler:
- // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
- // {
- // if (userDidSelect)
- // {
- // NSString* sid= printerController.printInfo.printerID;
- // UIPrintPaper*paper= printerController.printPaper;
- //
- // }
- // }];
-
-
- DebugLog(@"AIRPRINTER NOT AVAILABLE");
-
-
-
- if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
- {
- UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
- // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
-
-
-
- // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
-
-
-
- [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
-
-
-
- // [printPicker presentAnimated:YES completionHandler:
- ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
- {
- if (userDidSelect)
- {
- //User selected the item in the UIPrinterPickerController and got the printer details.
-
- [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
-
- //Here you will get the printer and printer details.ie,
- // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
-
-
-
-
- appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
-
- [self startAirPrintWithData:data];
-
- }
- else
- {
- appDelegate.printerURL = nil;
-
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
- // {
- //
- // [((MainViewController*)appDelegate.main_vc) Loginout:false];
- // }
- // [self prepareReturn:nil];
-
- //[self.navigationController popViewControllerAnimated:true];
- }
- }];
- }
- }
- }];
-
- // [waitalert dismissViewControllerAnimated:YES completion:nil];
-
- }];
- }
-
-
-
-
-
-
-
-
-
-
-
- }];
-
-
-
-
-
-
-
-
-
-
-
-
- }
- #pragma mark - UIPrintInteractionControllerDelegate
- - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
- {
- ;
- // (int) width = 29700
- // Printing description of (*( (int *)0x7caa3a40)):
- // (int) height = 42000
- //设置纸张大小
- CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
- for(UIPrintPaper* paper in paperList)
- {
- if (CGSizeEqualToSize(paper.paperSize, paperSize))
- return paper;
- }
-
- // UIPrintPaper * pp=[[UIPrintPaper alloc] init];
-
- // CGSize paperSize = CGSizeMake(612, 792);
- UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
- return p;
- }
- - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
- {
- }
- - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
- {
- }
- - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
- {
- }
- - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
- {
- }
- - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
- {
- }
- - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
- {
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
- // {
- //
- // [((MainViewController*)appDelegate.main_vc) Loginout:false];
- // }
- //
- // [self prepareReturn:nil];
- if(self.autoPrint)
- [self.navigationController popViewControllerAnimated:true];
- }
- @end
|