5

既知のprinterIdを使用して、選択したプリンターが表示されたときにUIPrintInteractionControllerに強制する方法が必要です。

注:テストを行うために、「プリンタ」を共有する「MacBookPro」にインストールされているPrintopiaを使用しています。

私はこのテストを行いました:

-(IBAction)print:(id)sender
{
 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];

 UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
      NSLog(@"Selected Printer ID: %@",printController.printInfo.printerID);      
  };

 NSString* path = [[NSBundle mainBundle] pathForResource:@"TestImage" ofType:@"png"];
 NSURL* imageURL = [NSURL fileURLWithPath:path isDirectory:NO];

 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
 printInfo.outputType = UIPrintInfoOutputPhoto;
 printInfo.jobName = @"Image print";
 controller.printInfo = printInfo;

 controller.printingItem = imageURL;

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
 {
    [controller presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];  // iPad
 }
 else
 {
     [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone
 }
}

印刷が完了すると、アプリは次のプリンターIDをログに記録します。

\032Printer\032@\032MacBook\032Pro._ipp._tcp.local.

プリンターをオーバーライドしたいので、次のようにします。

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.printerId = @"\032Printer\032@\032MacBook\032Pro._ipp._tcp.local.";
controller.printInfo = printInfo;

しかし、何らかの理由で機能しないため、UIPrintInteractionControllerは期待どおりにプリンターを選択しませんが、プリンターはプリンターリストに表示されます。

問題は、プリンタIDに存在する奇妙な文字にあると思います。

printInfo.printerIdがどのようにエンコードされ、手動で設定するかを知っている人はいますか?

NSString * printerIdをivarに保存し、次の印刷アクションで再度設定すると機能しますが、手動でプリンターIDごとにデフォルトのプリンターを強制することはできません。


ところで:明らかに、プリンターが利用できない/到達できない場合、私はそれを選択できないことを知っています...

4

1 に答える 1

7

デフォルトのプリンタをプログラムで設定するには、printInfo の printerID を ._ipp._tcp.local に設定するだけです。printerName は、UIPrinterInteractionController ポップオーバーのプリンターのリストに表示されている方法とまったく同じである必要があります。たとえば、LANIERCOLOR315 [00:80:A3:95:2D:41]と表示されているプリンターの場合、printerID はLANIERCOLOR315 [00:80:A3:95:2D:41]._ipp._tcp.localです。特殊文字をエンコードする必要はありません。フレームワークがそれを行います。

于 2012-10-18T17:59:28.873 に答える