Zu allererst hinzufügen UIPrintInteractionControllerDelegate und Variable erstellen
UIPrintInteractionController *printController;
Im Folgenden Code alle Bilder, Dokumente zu drucken, Excel, Powerpoint arbeitet PDF-Dateien für mich:
[self printItem:SomeData withFilePath:YourFilePath];
Im obigen Code geben Sie Ihre NSData Ihres Dokuments/Bild und URL (filePath) und unter weiterem Code von printItem: withFilePath: Methode
-(void)printItem :(NSData*)data withFilePath:(NSString*)filePath{
printController = [UIPrintInteractionController sharedPrintController];
printController.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [NSString stringWithFormat:@""];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
printController.printInfo = printInfo;
printController.showsPageRange = YES;
//If NSData contains data of image/PDF
if(printController && [UIPrintInteractionController canPrintData:data]) {
printController.printingItem = data;
}else{
UIWebView* webView = [UIWebView new];
printInfo.jobName = webView.request.URL.absoluteString;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
printController.printFormatter = webView.viewPrintFormatter;
}
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
//NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
}
};
// Check wether device is iPad/iPhone , because UIPrintInteractionControllerDelegate has different methods for both devices
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[printController presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
}
else {
[printController presentAnimated:YES completionHandler:completionHandler];
}
}
Ich hoffe, es hilft. Danke
Ich aktualisierte meine Antwort, um Beispielcode für den Druck des einfachen Textes einzuschließen. –