Step 1:
Start Xcode and make a New Project – Select View-based Application – I call it AirPrinting.

Add an image to your project – I added demo.png – Here is the image
Step 2:
Go to AirPrintingViewController.h and add the following code
#import <UIKit/UIKit.h>@interface AirPrintingViewController : UIViewController <UIPrintInteractionControllerDelegate>{}-(void)printItem;@end |
Next in AirPrintingViewController.m add the following code
#import "AirPrintingViewController.h"@implementation AirPrintingViewController-(void)printItem { NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"png"]; NSData *dataFromPath = [NSData dataWithContentsOfFile:path]; UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController]; if(printController && [UIPrintInteractionController canPrintData:dataFromPath]) { printController.delegate = self; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = [path lastPathComponent]; printInfo.duplex = UIPrintInfoDuplexLongEdge; printController.printInfo = printInfo; printController.showsPageRange = YES; printController.printingItem = dataFromPath; 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); } }; [printController presentAnimated:YES completionHandler:completionHandler]; }}- (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn addTarget:self action:@selector(printItem) forControlEvents:UIControlEventTouchDown]; [btn setTitle:@"PRINT" forState:UIControlStateNormal]; btn.frame = CGRectMake(0, 100, 320, 50); [self.view addSubview:btn];}@end |
Step 3:
Build and Run – You will see a print button – Click the print Button and you will see UIPrintInteractionController popup – Go ahead and Print.
Here is the entire Code

No comments:
Post a Comment