通过Xcode在App中打开打印对话框?

时间:2013-01-24 01:30:05

标签: ios objective-c xcode airprint

我是iOS编程的新手,遇到了一个问题。我有一个pdf,我想打印离开互联网。如何在没有第三方应用程序的情况下通过我的应用程序打印此内容?此外,我听说过苹果的AirPrint,它已经预先安装在iOS设备上。我该怎么用?感谢。

1 个答案:

答案 0 :(得分:2)

.H文件代码

 #import
    @interface xyviewController : UIViewController <...,UIPrintInteractionControllerDelegate> {

    UIPrintInteractionControllerDelegate should be delegate
    }
    -(IBAction)printdoc;

.M文件代码

-(IBAction)printdoc
{



NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile: path];


UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

if(pic && [UIPrintInteractionController canPrintData: myData] ) {

pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = myData;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
//self.content = nil;
if (!completed && error) {
NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
}
};

[pic presentAnimated:YES completionHandler:completionHandler];

}

}