0

基本上,我想在我的 Web 视图中单击一个按钮,该按钮将从远程网站下载 PDF 并使用关闭/返回按钮显示它。我正在使用 QLViewController,因此人们可以捏缩放 PDF,并且我正在使用带有 XCode 5 的 PhoneGap 3.0 对其进行编码。

这是我的插件的代码,它将显示带有后退按钮的新视图,但我似乎无法显示 PDF。有任何想法吗?

#import "PDFViewer.h"
#import <Cordova/CDV.h>

@implementation PDFViewer

- (void)loadRemotePdf:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString *website = [command.arguments objectAtIndex:0];
    NSString *filename = [command.arguments objectAtIndex:1];

    if (website != nil && [website length] > 0) {

        CGRect rect = [[UIScreen mainScreen] bounds];
        CGSize screenSize = rect.size;
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];

        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
        webView.autoresizesSubviews = YES;
        [webView canGoBack];
        webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

//        NSURL *myUrl = [NSURL URLWithString:website];
//        NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];

//        [webView loadRequest:myRequest];

//        [window addSubview: webView];

        NSURL  *url = [NSURL URLWithString:website];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        if ( urlData )
        {
            NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];

            filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, filename];
            [urlData writeToFile:filePath atomically:YES];

            // Create test view controller
            QLPreviewController *previewer = [[QLPreviewController alloc] init];
            previewer.dataSource = self;
            previewer.delegate = self;

            // Create navigation controller
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:previewer];

            [window addSubview: [nav view]];
            [window makeKeyAndVisible];
        }


        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    }
    else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];

    }

    // return result
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
{
    NSURL *fileURL = [NSURL fileURLWithPath: filePath];
    return fileURL;
}
@end
4

1 回答 1

0

是的,这可以通过 UIWebView 来完成。

如果您尝试显示驻留在某处服务器上的 PDF 文件,您可以简单地将其直接加载到您的 Web 视图中:

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSURL *targetURL = [NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];

//或者,如果您的应用程序捆绑了一个 PDF 文件(在此示例中名为“document.pdf”):

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];
于 2013-10-19T07:37:02.610 回答