从UIWebView打印-Swift

从UIWebView打印-Swift,swift,uiwebview,Swift,Uiwebview,我需要通过iPad应用程序上的UIWebView在网页上查看表单。用户提交表单后,我需要将打印作业从网页上的表单发送到与iPad位于同一wifi网络上的支持AirPrint的打印机。我可以在网站上做任何我需要做的事情。我读到UIWebView无法识别window.print()JavaScript命令 如何让iPad应用程序接收打印作业并通过AirPrint打印? 我看到这个网站上的其他人也问过类似的问题,但没有一个得到过详细的回答。苹果公司有一个打印UIwebView内容的示例项目 - (I

我需要通过iPad应用程序上的UIWebView在网页上查看表单。用户提交表单后,我需要将打印作业从网页上的表单发送到与iPad位于同一wifi网络上的支持AirPrint的打印机。我可以在网站上做任何我需要做的事情。我读到UIWebView无法识别window.print()JavaScript命令

如何让iPad应用程序接收打印作业并通过AirPrint打印?

我看到这个网站上的其他人也问过类似的问题,但没有一个得到过详细的回答。

苹果公司有一个打印UIwebView内容的示例项目

- (IBAction)printWebPage:(id)sender
{
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    if(!controller){
        NSLog(@"Couldn't get shared UIPrintInteractionController!");
        return;
    }

    UIPrintInteractionCompletionHandler completionHandler =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if(!completed && error){
            NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
        }
    };


    // Obtain a printInfo so that we can set our printing defaults.
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    // This application produces General content that contains color.
    printInfo.outputType = UIPrintInfoOutputGeneral;
    // We'll use the URL as the job name.
    printInfo.jobName = [self.urlField text];
    // Set duplex so that it is available if the printer supports it. We are
    // performing portrait printing so we want to duplex along the long edge.
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    // Use this printInfo for this print job.
    controller.printInfo = printInfo;

    // Be sure the page range controls are present for documents of > 1 page.
    controller.showsPageRange = YES;

    // This code uses a custom UIPrintPageRenderer so that it can draw a header and footer.
    APLPrintPageRenderer *myRenderer = [[APLPrintPageRenderer alloc] init];
    // The APLPrintPageRenderer class provides a jobtitle that it will label each page with.
    myRenderer.jobTitle = printInfo.jobName;
    // To draw the content of each page, a UIViewPrintFormatter is used.
    UIViewPrintFormatter *viewFormatter = [self.myWebView viewPrintFormatter];

#if SIMPLE_LAYOUT
    /*
     For the simple layout we simply set the header and footer height to the height of the
     text box containing the text content, plus some padding.

     To do a layout that takes into account the paper size, we need to do that
     at a point where we know that size. The numberOfPages method of the UIPrintPageRenderer
     gets the paper size and can perform any calculations related to deciding header and
     footer size based on the paper size. We'll do that when we aren't doing the simple
     layout.
     */
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT];
    CGSize titleSize = [myRenderer.jobTitle sizeWithFont:font];
    myRenderer.headerHeight = myRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;
#endif
    [myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
    // Set our custom renderer as the printPageRenderer for the print job.
    controller.printPageRenderer = myRenderer;

    /*
     The method we use to present the printing UI depends on the type of UI idiom that is currently executing. Once we invoke one of these methods to present the printing UI, our application's direct involvement in printing is complete. Our custom printPageRenderer will have its methods invoked at the appropriate time by UIKit.
     */
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [controller presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];  // iPad
    }
    else {
        [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone
    }
}