首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动AirPrint IOS

自动AirPrint IOS
EN

Stack Overflow用户
提问于 2016-05-11 03:10:30
回答 1查看 591关注 0票数 1

我需要在没有用户交互的情况下在Xcode中使用AirPrint。我不在乎我是否必须使用第三方框架来做到这一点。下面是我的代码,但需要用户的交互。

代码语言:javascript
复制
 NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@",TextField.text];

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    pic.delegate = self;


    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"PrintJob";
    pic.printInfo = printInfo;


    UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
    textFormatter.startPage = 0;
    textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0);
    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);
    }
};


    [pic presentAnimated:YES completionHandler:completionHandler];

如果有人能帮忙,那就太好了!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-12 19:24:24

我找到了一段视频,很有帮助!这是iOS 8的wwdc视频,他们刚刚推出了新的打印方式,而不用UIPrintInteractionController。这就是了。

代码语言:javascript
复制
    -(IBAction)print:(id)sender{

      NSMutableString *printBody = [NSMutableString stringWithFormat:@"Hey this is a test lets hope it works!"];

      UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
      pic.delegate = self;


      UIPrintInfo *printInfo = [UIPrintInfo printInfo];
      printInfo.outputType = UIPrintInfoOutputGeneral;
      printInfo.jobName = @"PrintJob";
      pic.printInfo = printInfo;


      UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
      textFormatter.startPage = 0;
      textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0);
      textFormatter.maximumContentWidth = 6 * 72.0;
      pic.printFormatter = textFormatter;
      pic.showsPageRange = YES;
//save your printer using code also in video. 

      UIPrinter *SavedPrinterUserDefaults = [[NSUserDefaults standardUserDefaults]
                                             objectForKey:@"SavedPrinter"];
      [pic printToPrinter:SavedPrinterUserDefaults
        completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
          if (completed && !error)
            NSLog(@"YAYA! it printed");
        }];

    }

我希望这对任何有同样问题的人都有帮助。下面是关于如何使用这个新API的更多信息

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37152468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档