1

パスワードで保護された PDF/DOC ファイルを UIWebview で開こうとしています。セキュリティ上の懸念により、サーバーからファイルを取得してメモリ (つまり NSData) に保存することしかできず、ファイルとして保存することはできません。

UIWebiew で loaddata 関数を使用しようとしましたが、パスワードで保護されていない状態で doc/xls/ppt/pdf をロードすることしかできません。

パスワード付きの doc/xls ファイルの場合、「ドキュメントを読み取れません。操作を完了できませんでした (QuickLookErrorDomain エラー 44820/912)」と表示されます。UIWebview でファイルを開く方法を教えてください。

パスワード付きのPDFファイルの場合、「ドキュメント「空白」はパスワードで保護されています」と表示されます。「空白」の代わりに正しいドキュメント名を表示する方法を教えてください。

cgpdfも検索し、pdfファイルをCGPDFDocumentRefとしてロック解除する方法を知っていますが、NSDataに戻してUIWebviewに直接入力する方法を見つけることができません。

ありがとう。

4

2 に答える 2

1

ファイルをローカルに書き込み、データを読み取り、Web ビューで表示する

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

ファイルのデータをローカルに書き込まずに更新 する. データをメモリで処理してpdfファイルをwebviewで表示する.

@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);
}
于 2016-04-18T08:19:58.310 に答える
0

保護された PDF ファイルのロックを解除するには、CGPDFDocumentを使用する必要があります。

次にCGPDFDocumentUnlockWithPasswordを使用してファイルのロックを解除できます.UIWebViewははるかに簡単ですが、ケースではCGPDFDocument

ここにあなたに役立つライブラリがあります

于 2016-04-18T09:14:55.183 に答える