ios - Objective-C : View PDF File Link With UIWebView

标签 ios objective-c pdf uiwebview nsurlrequest

我正在尝试从服务器查看 PDF 文件,但我只看到黑色的 View ,不包含任何内容。这是我的代码:

    pdfViewer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 768, 949)];
    pdfViewer.backgroundColor = [UIColor whiteColor];
    pdfViewer.delegate = self;
    [sgContainer addSubview:pdfViewer];

    chatSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    chatSpinner.frame = CGRectMake(334, 462, 100, 100);
    [pdfViewer addSubview:chatSpinner];

    NSString *httpSource = @"http://www.mywebsite.com/folder/subfolder/file.pdf";
    NSURL *fullUrl = [NSURL URLWithString:httpSource];
    NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
    [pdfViewer loadRequest:httpRequest];

最佳答案

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

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];

如果您遇到任何问题,请告诉我。

关于ios - Objective-C : View PDF File Link With UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17205350/

相关文章:

iphone - 键盘在 UIScrollView 中隐藏文本字段。如何根据键盘移动文本字段?

objective-c - Core Data 应用程序中的多个 View 和源列表

php - FPDF : FilterFlateDecode: invalid stream data 中的 FPDM 问题

android - 使用外部应用程序 android 打开保存在 res/raw 目录中的 pdf 文件

ios - PFQuery其中whereKey是指针

ios - 无法更改后退按钮标题,但我可以隐藏它

ios - 模拟器开始在水平 View 中打开。如何让它回到默认的纵向 View ?

ios - RXswift,重复可观察直到完成

ios - 以编程方式将 UIButton 元素添加到 UIView 不会显示

ios - 获取本地 PDF 页面高度 - iOS