| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- //
- // 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"
- @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.webView.scrollView addSubview:ref];
-
-
- self.webView.dataDetectorTypes=UIDataDetectorTypeAll;
-
-
-
-
- // 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
- {
-
- UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Loading..."];
- self.mum.hidden=false;
-
- 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 dismissWithClickedButtonIndex:0 animated:FALSE];
-
- 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.webView loadRequest:request];
-
- }
- else
- 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 dismissWithClickedButtonIndex:0 animated:FALSE];
-
- [[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.webView 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 {
-
-
- // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." 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 dismissWithClickedButtonIndex:0 animated:FALSE];
- //
- // 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];
-
- 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 - web view delegate
- - (void)webViewDidFinishLoad:(UIWebView *)webView
- {
- self.mum.hidden=true;
-
-
- if(self.onLoadSuccess)
- self.onLoadSuccess(self.url);
-
- if(self.autoPrint)
- [self startAirPrintWithData:self.content];
-
- }
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
- {
-
- }
- #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];
- }
- /*
- #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
- //{
- //
- //
- // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." 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 dismissWithClickedButtonIndex:0 animated:FALSE];
- //
- // 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
- {
-
-
- UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." 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];
-
-
- if(appDelegate.printerURL==nil)
- {
- [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
- 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];
- }
- }];
- }
- }
- else
- {
- UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
- [airPrinter contactPrinter:^(BOOL available)
- {
- [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
-
- 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];
- }
- }];
- }
- }
- }];
- }
-
-
-
-
-
-
-
-
-
-
- }
- #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
|