2013-06-06 28 views
7

Tôi đang cố gắng hiển thị tệp PDF trong ứng dụng iPad của mình thông qua UIWebView như được hiển thị bên dưới.Làm thế nào để hiển thị NSData với nội dung PDF trong UIWebView của iOS?

NSData * responseData = [NSURLConnection sendSynchronousRequest:mutableUrlRequest returningResponse:nil error:nil]; 
[pdfWebView loadData:responseData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; 

Nhưng điều này không hiệu quả. Đó có phải là cách phù hợp để hiển thị không? Nếu không ai có thể hướng dẫn tôi hiển thị tệp PDF trong UIWebView hoặc bất kỳ phương thức hữu ích nào khác.

Cảm ơn!

+0

Bất kỳ lý do tại sao bạn làm không tải trực tiếp url pdf trong chế độ xem web? –

+0

@verbumdei Lý do là, Yêu cầu của tôi là lấy dữ liệu PDF bằng cách nhấn Java Servlet. –

Trả lời

21

Got câu trả lời cho câu hỏi của tôi. Dòng dưới đây sẽ làm phép thuật.

[webview loadData:PdfContent MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; 
2

Lưu tệp vào một tệp có hậu tố PDF trong thư mục tạm thời và mở tệp đó từ đó.

+1

Bạn có thể vui lòng cung cấp một số đoạn mã để lưu NSData với mảng byte để lưu dưới dạng pdf trong thư mục cục bộ. –

0

nếu bạn có một tập tin PDF đi kèm với ứng dụng của bạn (trong ví dụ này có tên là "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]; 

Bạn có thể tìm thêm thông tin ở đây: QA1630 kỹ thuật: Sử dụng UIWebView để hiển thị chọn loại tài liệu .

+2

Câu hỏi của tôi là khác nhau, tôi đang có dữ liệu của PDF từ máy chủ. –

2

Làm thế nào để tiết kiệm NSData (PDF File) vào thư mục tài liệu:

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *documentFolderPath = [searchPaths objectAtIndex: 0]; 
    NSString *imagePath = [documentFolderPath stringByAppendingPathComponent:@"yourPdfFileName.pdf"]; 

NSFileManager *filemanager=[NSFileManager defaultManager]; 
    if(![filemanager fileExistsAtPath:filePath]) 
     [[NSFileManager defaultManager] createFileAtPath:filePath contents: nil attributes: nil]; 

    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath]; 

    [handle writeData:data]; 
0

Bất kỳ lý do nào bạn không sử dụng Khung nhìn nhanh hoặc UIDocumentInteractionController? Họ có thể cung cấp cho người dùng nhiều chức năng hơn một WebView và một chút ít cloogy.

Dưới đây là các liên kết đến tài liệu hướng dẫn của Apple: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/UsingtheQuickLookFramework.html

https://developer.apple.com/library/ios/documentation/uikit/reference/UIDocumentInteractionController_class/Reference/Reference.html

Nếu bạn đang tìm kiếm để làm cho hệ thống tập tin PDF từ các ứng dụng, không cần phải băn khoăn! Chỉ cần chắc chắn rằng bạn đã lưu trữ pdf có một phần mở rộng thích hợp '.pdf', sau đó cung cấp cho nó vào QLPreviewController trong đường dẫn đầy đủ:

self.contentURL = [NSURL fileURLWithPath:fullPath]; 
2

SWIFT3:

webView.load(data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: URL(string: "You can use URL of any file in your bundle here")!)