我正在使用UIWebView
控件并仅在横向模式下打开网页网址。在特定事件的此URL中,PDF正在打开。这次PDF没有正确打开(它总是只显示PDF文件的第一页),而在输出窗口中我得到“flatedecode decode error”。
有没有办法让我的应用程序可以捕获PDF开放事件,我可以在Safari或其他浏览器而不是UIWebView
打开PDF?
或者在Phonegap中有什么方法可以达到这个目的吗?
答案 0 :(得分:0)
我想您只需要找出是否点击打开PDF的特定链接?
确保将viewcontroller设为webview的委托。然后实现这个方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
当链接点击加载新页面时, navigationType 将 UIWebViewNavigationTypeLinkClicked 。
请求会告诉您正在打开的网址,如果网址是您的PDF网址,您可以收听。如果是,那么您可以在方法中处理它,但是您想要 - 下载它并在另一个Web视图中打开或在Safari中打开。
更多详情here。
答案 1 :(得分:-1)
您可以使用此代码
将PDF保存在文档目录中NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
//You must check if this directory exist every time
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO)
{
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:@"QRScanner.pdf"];
//webView.request.URL contains current URL of UIWebView, don't forget to set outlet for it
NSData *pdfFile = [NSData dataWithContentsOfURL:webView.request.URL];
if(pdfFile)
{
[AppDelegate obj_appDelegate].Alertview.hidden = YES;
[[AppDelegate obj_appDelegate].activity stopAnimating];
}
[pdfFile writeToFile:filePath atomically:YES];