AirPrint连接拒绝错误

Sam*_*cer 6 printing error-handling objective-c airprint ios5

在我的iOS应用程序中,我有以下代码用于AirPrinting一个简单的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];
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行我的项目(测试它)时,当我点击UIPrintInteractionController上的"Print"时,这是我在输出调试器窗口中得到的错误:

Simulated\032InkJet\032@\032USER\032NAME\032iMac._ipp._tcp.local.: startJob: Unable to connect to printd: Connection refused
Run Code Online (Sandbox Code Playgroud)

我使用打印模拟器在iOS 5.1模拟器中收到此错误.为什么我收到此错误?我觉得这与我如何使用打印模拟器有关.

任何帮助是值得赞赏的,并且作为附注,有没有人知道如何从iPad上的普通UIButton而不是BarButtonItem显示UIPrintInteraction控制器?

编辑:应该注意的是,在iOS 6.0+中使用Share表时会自动设置AirPrint.

小智 11

我有完全相同的问题,基本上mincemeat的答案解决了这个问题.但是我确实必须首先跳过几个篮球.最终它是某种文件权限问题.

我做了以下(注意,需要管理员访问):

  1. 打开终端并导航到文件夹/ p​​rivate/var/tmp /

  2. 输入'whoami'以查看您确切的用户名.

  3. 假设您的用户名是'dogtest'.输入'sudo chown dogtest:admin printd'.你会得到一个警告,基本上要求你确保你知道你在做什么.

  4. 输入您的管理员密码,然后按Enter键.

  5. 现在您拥有了printd文件的所有权,并且可以随心所欲地使用它.通过键入"mv printd printd-ren"将文件重命名为其他名称.

  6. 打开iOS模拟器和打印机模拟器.

  7. 在iOS模拟器中打开Safari并转到任何网页.

  8. 点击底部中心的"分享"按钮,然后选择"打印".

  9. 按照提示完成打印.您应该在Printer Simulator日志窗口中看到更多活动.此时,使用您的帐户权限重新创建printd文件(这很好).

  10. 退出iOS Simlulator和Printer Simulator.

  11. 在模拟器中重新运行您的iOS应用程序,然后重新打开打印机模拟器.

  12. 现在,您应该可以从应用程序模拟Air Print,打印输出将以PDF格式预览打开.

我希望这能为您解决问题,或者至少指出您正确的方向.祝好运.