-2
NSURL *url = [NSURL URLWithString:@"PDFLINK"];
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:url];
NSString * name = @"First";
// Store the Data locally as PDF File
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];



NSString *newFilePath = [documentsDirectory stringByAppendingPathComponent:@"my.pdf"];
NSError *error;
if ([fileManager createFileAtPath:newFilePath contents:pdfData attributes:nil])
{
    NSLog(@"Create Sucess");
    [self loadPDF];
}
else
{
    NSLog(@"Create error: %@", error);
}

}


-(void)loadPDF
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSArray *resultArray = [fileManager subpathsOfDirectoryAtPath:documentsDirectory error:nil];
    if (![resultArray containsObject:arrObj])
    {
        // do something
    }
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[resultArray objectAtIndex:0]];

   // Now create Request for the file that was saved in your documents folder
   //NSLog(@"The filePath %@",filePath);
   NSURL *url = [NSURL fileURLWithPath:filePath];
   NSLog(@"The url %@",url);
   NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
   [_webView setUserInteractionEnabled:YES];
   [_webView setDelegate:self];
   [_webView loadRequest:requestObj];
}

Here is my code.I have more 10 pdf and want to store them locally on device and display next time without download.

It saves the pdf again and i cannot check whether it is downloaded or not ?

Please help me

1 Answers1

0

I am not sure what you mean but, to download a file , you need to use NSData:

   NSString *stringURL = @"your file URL";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
  NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString  *documentsDirectory = [paths objectAtIndex:0];  

  NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.extension"];
  [urlData writeToFile:filePath atomically:YES];
}

Now the downloaded file exists at filePath and you can get it by using:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"filename with extension"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

you can try this to check if file exists at file path:

 NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

   NSString* file = [documentsPath stringByAppendingPathComponent:@"filename"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];

 if(fileExists){
         //dont download, just grab it and display it
   }
     else{
      //download and display it
    }
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • Hi, Could you help me download file if it does not exist and open the file from the path if the file is downloaded ? – user1859651 Sep 16 '15 at 14:48
  • Thanks .. it should help :) – user1859651 Sep 16 '15 at 15:01
  • NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.extension"]; – user1859651 Sep 16 '15 at 17:21
  • any issues with that line? – Teja Nandamuri Sep 16 '15 at 17:22
  • NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"%@",randomString]; I am tring to give new name each time but it says data argument cannot be use by format string – user1859651 Sep 16 '15 at 17:29
  • try this for generating random string: http://stackoverflow.com/questions/2633801/generate-a-random-alphanumeric-string-in-cocoa – Teja Nandamuri Sep 16 '15 at 18:06
  • Also could you explain why i am getting more down vote even though i edited my question and you are helping with correct solution ? – user1859651 Sep 16 '15 at 19:11
  • There are millions of developers out there in SO, and your question got viewed by 26 devs..... some of them didnt consider this as valuable question, because it contains lots of code and you didnt mention exactly what is wrong with your code.Moreover there are many answers similar to your question available in SO. Even I just copied a prt of my answer from some one's answer. You get downvote because your question lack reasearch – Teja Nandamuri Sep 16 '15 at 19:16
  • Dont worry about the downvotes. It is quite common in here. Try to ask more qualitative questions in future which shows your effort – Teja Nandamuri Sep 16 '15 at 19:17
  • Thank for your help and i would like to select your answer and give it a plus point .. but my score is low :( Thanks for all your help – user1859651 Sep 17 '15 at 09:20