0

问题:我尝试将数据加载到 web 视图,但只是得到一个黑屏。

我已成功下载 PDF 并将其存储在文档目录中,但是当我在 UIWebView 中查看它时,我什么也得不到。我怀疑这是因为数据(正如我在控制台中查看的那样)是十六进制的,并且我用来显示它的方法无法读取数据。这是我将数据加载到 webview 中的代码:

NSURL *url = [NSURL fileURLWithPath:PDFpathwithextension];

NSData *myData = [NSData dataWithContentsOfURL:url];
NSLog(@"data being loaded to webview =%@",myData);

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

这是从 NSlog 打印的数据示例:

"data being loaded to webview = b8e5f672 bec5562d 28b63505 9a2b7733 0595fb24 46eb982c d77a526e b02ecaf5 d69f227f 96e92b88 149118ca 09f33da2 677c8e7d 7707ebbf 889a73b5 e8776a00 86abd1ef 206d184f 0b9e8db5 0c381d2b a0b66e3d 543ba3c1 e64ca0ac ce34baac 2e9b6da9 2be01439 2c3c93c3 1e5050db 20ccabf5 88726af6 8ab36bfc 92aceae3 325df525 99b6fa27 c89f64da 2a22c9ac 22e24c3b 61747622 d2d99678 887d7713 57ce8ba8 75ba1eeb e044bf51 8739405a 907afc57 d31008f6 a697c1ea 0a817257 1494bae2 a8129786 36bb74ac 42571e3b bfa9889b db64e519 9aea02b2 1bdd425d e31e91b6 61589cde 3023496d 5890a634 dc976aea ff284971 12714a1d 6152eb88 28d5f182 07d877d7 3107f3a8 7d1cad80 bf09fba0 11a01d69 c06f7b0b fa1e370f 2cbb6450 dcfa2a98 3de150e8 89817ccf 762acf93 41e778f4 2c7d6b21 3babb58c 93d95ac3 cbd8d512 90bacb2b d4b88702 d5ee6966 bbfb3c93 e8becb24 b81f3189 2dff1125 361351a2 8b046e7f 46d31277310797b1 0673a83f e546df8b 56a01b71 23b5b88e 97b60198 3a283076 3290dbb5 1c72bca1 a0f76e82 1dddf190 d5ada132 bb757486 d748a779 4bd81aaf 9da3f636 71b77775 f0555dfb 03e2ba26 05dbbace 0963bbee 08633bbf 16c6eefe b730b69d 0863db88 60abe705 37b1ff16 f0ceb31e 8071d4eb 4776234d ed00b60e d4ef0630 f402e8fa 0490b12f 08d2fb57 43aa2f1c 527c4ad0 f812413d 904e250d e8a9449f 8956f92a 58713e27 3bd6e7e1 287dfddc 2dbec3bc cdfd6779 51bedbbc a8feaf78 517ddff1 a37a093f 6a0fd2f3 82ab58f3 b378df63 5e80912e 803dc82e fc76a076 296ae7f5 03640e02 a80fd290 30c240fc c82b1077 2818b61d 52c0567f 2cc4fa93 20c6af85 e8d15cd8 325a426d 1aada636 8eeea415 fe5e56b8 7f8cb5c1 7f86b5de ff1e7bbd ffb7acb0 917fb1c3 0e124ed8 1072e005 1731e7b3 a87ba40f 7b11ed69 27d288df 953ecc01 6aef406b a4190588 9b00d83c c983c8a3 12501c5d 0e115321 103e1509 1ba6b7c2 6bd36a58 3fad83b0 37fec776 7940457d 6561fcfb4f1f0670 e87d5004 2c048809 8385b180 0a28a271 5dd7e4e8 d1d5a0c6 8206bbb8 08414445 542c188a 4e5058aa 1d18d468 041d7b2c 472431ae 71755d13 7bd65812 ebdb8fe2 eeead9c3 f9f1a8ef 7bb7bcfb ee1d8940 f3780498 67a0a379 b1e467ce 91da9b8b 255ff33e c960fe51 326cfa5d 66281032 43be901b..."

4

2 回答 2

0

您没有使用 Quick Look Framework 或 UIDocumentInteractionController 的任何原因?它们能够为用户提供比 WebView 更多的功能和更少的混乱。

以下是 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

如果您希望从应用程序的文件系统呈现 PDF,则无需担心!只需确保您存储的 pdf 具有正确的“.pdf”扩展名,然后以完整路径将其提供给 QLPreviewController:

    self.contentURL = [NSURL fileURLWithPath:fullPath];
于 2013-11-05T23:33:28.467 回答
0

PDFpathwithextension网址还是本地路径?
如果是网络,首先保存myData到磁盘并尝试:

NSString *path = @"get full local path of your file including filename.extension";
NSURL *url = [NSURL fileURLWithPath:path];
[UIWebView alloc] loadRequest:[NSURLRequest requestWithURL:url]];

如果您不想下载它,那为什么要使用NSData
只需将 pdf 网址加载到UIWebView.

[uiwebview loadrequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:PDFpathwithextension]]];
于 2013-10-03T21:18:58.390 回答