开发者

AirPrint for iPhone App

I am trying to implement AirPrint into my application. At the moment, my code is this:

AirPrintingViewController.h

#import <UIKit/UIKit.h>

@interface AirPrintingViewController : UIViewController <UIPrintInteractionControllerDelegate>{

}

-(void)printItem;

@end

AirPrintingViewController.m

#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.du开发者_JS百科plex = 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

When I run the application, I press the button and it takes me to a print page. When I enable printer simulator and then press print, nothing happens...

I tried to airprint through my iPhone to the printer simulator and it worked.

Am I doing something wrong?

Thanks.


Are you sure the picture is found?

Try logging the path and dataFromPath.

NSLog(@"path = %@", path);
NSLog(@"dataFromPath = %@", dataFromPath);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜