iOS支持AirPrint的uiwebview内容

时间:2013-09-19 16:56:01

标签: iphone xcode printing uiwebview airprint

我是XCode和App开发的新手。我目前正在iPad上的uiwebviews中加载基于Web的应用程序。加载一个特定页面时,它会显示一个pdf文件。 我希望能够使用AirPrint打印此pdf文件。我正在寻找一个简单的解决方案。目前我正在处理的应用程序有6个文件,它使用。

-ViewController.m
-ViewController.h
-MainStoryboard_iPad.storyboard
-MainStoryboard_iPhone.storyboard
-AppDelegate.h
-AppDelegate.m

在MainStoryboard文件中,有许多窗口(图形),它们是中央导航系统所喜欢的。如果有可能花一些时间来真正解释我需要做什么,而不是'看看这个链接'。我有编程经验,但从未使用过XCode或任何与Apple相关的产品。

1 个答案:

答案 0 :(得分:6)

我想出了如何做到这一点。首先我在这里找到了一段代码,iOS Air print for UIwebview,我当时不知道如何实现这个,但后来我做了如下。

我的应用程序是单视图XCode项目

在我的故事板中,我插入了一个按钮(在我的导航栏上)并将其标识符更改为“操作”,然后确保打开“tuxedo”编辑器视图,显示我的ViewController.m文件,单击并拖动按住控件的同时ViewController.m文件的按钮。在询问我的按钮id后插入了我的IBAction方法。

myActionButton

然后我复制了问题的答案3中指定的代码。我的ViewController.m看起来有点链接。

#import "ViewController.h"
@interface ViewController()
@end

@implementation ViewController()
//Some stuff here
@end

@synthesize webView

-(IBAction)myActionButton:(id)sender{
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = webView.request.URL.absoluteString;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.showsPageRange = YES;
pic.printFormatter = webView.viewPrintFormatter;
[pic presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
    // indicate done or error
}];
}

同样在我的ViewController.h文件中

#import ...
@interface ViewController : ...
{
IBOutlet UIWebView *webView
}

@property (nonatomic,retain) IBOutlet UIWebView *webView
@end

我没有设置网页浏览量,所以我不是100%确定它们是如何创建的,但是在HackLife87的youtube上为初学者设置了一个很好的系列,展示了如何制作单个视图应用。我认为XCode教程视频#7涉及设置视图。

由于我对XCode和IPad应用程序开发非常环保,我设法通过结合观看上述XCode教程视频的知识,然后通过Hafthor实现stackoverflow上提供的解决方案来解决我的问题。