1

How i can loading a File from NSHomeDirectory () in a UIWebview ? It's possible ? I mean loading .pdf files from the /Documents/ folder.

Thanks!

Marv
  • 153
  • 2
  • 9
  • Loading a PDF into a web view isn't optimal. Look into [**`QLPreviewController`**](http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/QLPreviewController_Class/Reference/Reference.html). – sudo rm -rf Jan 23 '12 at 00:23

2 Answers2

1

Here's how you can download, read and store your pdf locally in iphone application:

First create UIWebView and include <<UIWebViewDelegate>>

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  NSString *documentsPath = [paths objectAtIndex:0];
  NSString *filePath = [documentsPath stringByAppendingPathComponent:@"YourPDF.pdf"]; 
 if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
           // download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
         NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://s3.amazonaws.com/hgjgj.pdf"]];
            //Store the downloaded file  in documents directory as a NSData format
         [pdfData writeToFile:filePath atomically:YES];
    }

     NSURL *url = [NSURL fileURLWithPath:filePath];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [yourWebView setUserInteractionEnabled:YES];
     [yourWebView setDelegate:self];
     yourWebView.scalesPageToFit = YES;
     [yourWebView loadRequest:requestObj];

Or, if you simply want to read/load pdf from your Resource folder then simply do this :

     NSString*  filePath= [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"]];
    /// or you can even read docs file as : pathForResource:@"sample" ofType:@"docx"]
     NSURL *url = [NSURL fileURLWithPath:filePath];
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
     [yourWebView setUserInteractionEnabled:YES];
     [yourWebView setDelegate:self];
     yourWebView.scalesPageToFit = YES;
     [yourWebView loadRequest:requestObj];
Patrick
  • 6,495
  • 6
  • 51
  • 78
Samir Jwarchan
  • 1,027
  • 11
  • 14
0

I don't know how dows NSHomeDirectory() works, but you can Look into this post to see how to get the full path of the files and this other to see how to load a PDF into a UIWebView

Community
  • 1
  • 1
Esepakuto
  • 1,332
  • 9
  • 12