Write file in local and read the data and show it in web view
//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];
Update
without writing the file data in local, Process the data in memory and show the pdf file in 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);
}