私のiOSアプリには、単純なNSStringのAirPrintingに使用されている次のコードがあります。
#pragma mark - Print
-(IBAction)print:(id)sender {
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Message";
pic.printInfo = printInfo;
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]initWithText:self.Message.text];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
pic.showsPageRange = YES;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Printing could not complete because of error: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
}
プロジェクトを(テストするために)実行すると、UIPrintInteractionControllerで[印刷]をタップしたときに出力デバッガウィンドウに表示されるエラーは次のとおりです。
Simulated\032InkJet\032@\032USER\032NAME\032iMac._ipp._tcp.local.: startJob: Unable to connect to printd: Connection refused
PrintSimulatorを使用しているiOS5.1Simulatorでこのエラーが発生します。なぜこのエラーが発生するのですか?プリントシミュレーターの使い方と関係がある気がします。
助けていただければ幸いです。補足として、BarButtonItemではなくiPadの通常のUIButtonからUIPrintInteractionコントローラーを表示する方法を知っている人はいますか?
編集:iOS 6.0以降で共有シートを使用すると、AirPrintが自動的にセットアップされることに注意してください。