.h
typedef NS_ENUM(NSInteger,XSPrintInfoOrientation) {
XSPrintInfoOrientationPortrait,
XSPrintInfoOrientationLandscape,
} ;
@interface PrintPreviewListVC: WViewController
@property ( assign , nonatomic ) XSPrintInfoOrientation orientation;
@property ( strong , nonatomic ) NSString * urlStr;
@property ( strong , nonatomic ) UIWebView * webView;
.m
@interface PrintPreviewListVC () < UIWebViewDelegate ,UIPrintInteractionControllerDelegate >
{
UIBarButtonItem * _rightItem;
}
@end
@implementation PrintPreviewListVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_rightItem = [self showRightBtn:@"打印"];
self.webView = [[UIWebView alloc]initWithFrame:RECT(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -64 ) ];
[_webView loadRequest:[NSURLRequest requestWithURL:URL(_urlStr)]];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
[self.view addSubview:_webView];
[self.view gifViewFrame:RECT(0, 0 , SCREEN_WIDTH, SCREEN_HEIGHT - 64 ) retryBlcok:^{
[_webView reload];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setUrlStr:(NSString *)urlStr{
_urlStr = urlStr;
}
- (void)rightItemClick{
UIPrintInteractionController * printC = [UIPrintInteractionController sharedPrintController];
if ( !printC ) {
NSLog(@"没有打印功能");
return;
}
printC.delegate = self;
UIPrintInfo * printInfo = [UIPrintInfo printInfo];
printInfo.jobName = @"XS";
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = _orientation;//UIPrintInfoOrientationLandscape;
printC.showsPageRange = YES;
printC.printInfo = printInfo;
printC.printFormatter = [_webView viewPrintFormatter];
void(^completionHandler)(UIPrintInteractionController *,BOOL , NSError * ) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (completed)
{
// 执行成功后的处理
}
else if (!completed && error)
{
// 执行失败后的处理
}
};
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[printC presentAnimated:YES completionHandler:completionHandler];
// iPhone使用这个方法
}
}
#pragma mark ------- UIWebView Delegate -------
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[self.view LoadingState];
_rightItem.enabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.view FinishState];
_rightItem.enabled = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[self.view FailedState];
}