AirPrint Connection Refused Error

2019-02-09 20:01发布

问题:

In my iOS app, I have the following code that is being used for AirPrinting a simple NSString.

#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];
    }
}

When I run my project (to test it), this is the error I get in the Output Debugger Window when I tap "Print" on the UIPrintInteractionController:

Simulated\032InkJet\032@\032USER\032NAME\032iMac._ipp._tcp.local.: startJob: Unable to connect to printd: Connection refused

I get this error in the iOS 5.1 Simulator using the Print Simulator. Why am I getting this error? I have a feeling it has to do with how I am using the Print Simulator.

Any help is appreciated, and just as a side note, does anyone know how to display the UIPrintInteraction controller from a normal UIButton on the iPad instead of a BarButtonItem?

EDIT: It should be noted that AirPrint is automatically setup when using Share Sheets in iOS 6.0+.

回答1:

I had the exact same problem and basically mincemeat's answer fixed the issue. However I did have to jump through a few hoops first. Ultimately it was some sort of file permissions problem.

I did the following (note, admin access is required):

  1. Open Terminal and navigate to folder /private/var/tmp/

  2. type 'whoami' to see your exact user name.

  3. Let's say your user name is 'dogtest'. type 'sudo chown dogtest:admin printd'. You'll get a warning that basically asks you to make sure you know what you're doing.

  4. Enter your admin password and hit enter.

  5. Now you have ownership of the printd file and can do what you want with it. Rename the file to something else by typing 'mv printd printd-ren'.

  6. Open iOS Simlulator and Printer Simulator.

  7. Open Safari in iOS Simulator and go to any webpage.

  8. Tap the Share button at the bottom center and choose Print.

  9. Follow the prompts to complete the print. You should see a lot more activity in the Printer Simulator log window. At this point the printd file is recreated with your account permissions (that's good).

  10. Quit both iOS Simlulator and Printer Simulator.

  11. Re-rerun your iOS app in the simulator and then reopen Printer Simulator.

  12. Now you should be able to simulate Air Print from your app and the print-out will open in Preview as a PDF.

I hope this solves the problem for you or at least points you in the right direction. Good luck.



回答2:

Try removing

/private/var/tmp/printd



回答3:

I had printer simulator stop working for me and not give me any errors at all. I tried different simulators, resetting them, restarting the computer, killing printd as mentioned above, and other things too.

In the end, what fixed it for me was changing my network connections: at the time, i had wifi plus an ethernet cable (via thunderbolt) attached to my macbook -- both wifi + ethernet connected to the same network. I popped out the ethernet cable and immediately it started working again.

Thanks Apple, where can I get my 20 mins back :)