ios - 如何在不使用临时文件的情况下在 UIWebview 中打开受密码保护的 PDF/DOC?

标签 ios objective-c uiwebview cgpdf

我们正在尝试在 UIWebview 中打开受密码保护的 PDF/DOC 文件。出于安全考虑,我们只能从服务器检索文件并存储到内存中(即 NSData),而不能存储为文件。

我们尝试在UIWebiew中使用loaddata函数,它只能在没有密码保护的情况下加载doc/xls/ppt/pdf。

对于带密码的doc/xls文件,显示“Unable to Read Document. The operation couldn't be completed (QuickLookErrorDomain error 44820/912)。请问如何在UIWebview中打开文件?

对于带密码的pdf文件,它显示“文档“空白”受密码保护”。请问如何才能显示正确的文档名称而不是“空白”?

我也搜索了 cgpdf 并知道如何将 pdf 文件解锁为 CGPDFDocumentRef,但找不到将其改回 NSData 并直接输入到 UIWebview 的方法。

谢谢。

最佳答案

在本地写入文件并读取数据并在 WebView 中显示

//convert file data(ie : NSData) as string
NSData *fileData;//This is the data from your request
NSString *fileString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];

//get the file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"sample.pdf"];

//Save as file
NSError *error;
BOOL hasFileWritten = [fileString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

if(!hasFileWritten)
{
    NSLog(@"Write file error: %@", error);
}

//open pdf in webview
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

更新 不在本地写入文件数据,在内存中处理数据并在webview中显示pdf文件

@interface ViewController ()
{
    IBOutlet UIWebView *webView;
    CATiledLayer *tiledLayer;
    CGPDFPageRef pageRef;
}
@end


CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url);

BOOL success = CGPDFDocumentUnlockWithPassword(pdf, "test");

if(success)
{
    pageRef = CGPDFDocumentGetPage(pdf, 1);

    CGRect pageRect = self.view.frame;

    tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 1000;
    tiledLayer.levelsOfDetailBias = 1000;
    tiledLayer.frame = pageRect;

    UIView *contentView = [[UIView alloc] initWithFrame:pageRect];
    [contentView.layer addSublayer:tiledLayer];

    [webView addSubview:contentView];
}


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, layer.bounds, 0, true));
    CGContextDrawPDFPage(ctx, pageRef);
}

关于ios - 如何在不使用临时文件的情况下在 UIWebview 中打开受密码保护的 PDF/DOC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687901/

相关文章:

iphone - 无法从委托(delegate)调用 Controller 方法

iphone - 在哪里可以找到有关私有(private) iPhone API 的信息?

ios - 我想保存应用程序,这样当用户退出时,用户返回时 uitextfields 不会删除为文本

ios - 设置UILabel的frame时,是否可以在代码中将其设置到superview的垂直和水平中心?

ios - UIWebView webCore 在 iOS7.1 中崩溃(例如 : www. latimes.com)

css - 溢出 : scroll not working in UIWebView in iOS 8

ios - 如何将整个 XCode 项目切换到之前的提交

iphone - 向 UITextField 添加不可编辑的文本后缀

ios - 如何编写一种方法来控制所有标签或 textView?

ios - 提交查询后如何使搜索按钮起作用?