kunal
kunal

Reputation: 328

how to save pdf file in document directory

I am beginner of iPhone. I have no idea about how to save pdf file in document directory. I have make the folder in document directory. I have first open the pdf in Webview.but how to store pdf in document directory.

This is my code is...

- (void)viewDidLoad
{
    [super viewDidLoad];
 NSArray *path = [[NSBundle mainBundle] pathsForResourcesOfType:@"pdf" inDirectory:nil];
  UIBarButtonItem *tempButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"starbtnwhite.png"] style:UIBarButtonItemStylePlain target:self action:@selector(favbtnClicked:)];

    self.navigationItem.rightBarButtonItem = tempButton;

 NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *imagesFolderPath = [documentFolderPath stringByAppendingPathComponent:@"Favourite"]; 

    //Check if the videos folder already exists, if not, create it!!!
    BOOL isDir;

    if (([fileManager fileExistsAtPath:imagesFolderPath isDirectory:&isDir] && isDir) == FALSE) 
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:imagesFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }


if(sel_id==0)
    {
            NSURL *targetURL = [NSURL fileURLWithPath:[path objectAtIndex:0]];
            NSLog(@"targeUrl--%@",targetURL);

            NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
            NSLog(@"request--%@",request);

            [webView loadRequest:request];
            [self.view addSubview:webView];
//            [webView release];

    }
    else if(sel_id == 1)
    {
        NSURL *targetURL = [NSURL fileURLWithPath:[path objectAtIndex:1]];
        NSLog(@"targeUrl--%@",targetURL);

        NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
        NSLog(@"request--%@",request);

        [webView loadRequest:request];
        [self.view addSubview:webView];
//        [webView release];

    }
    else if(sel_id == 2)
    {
        NSURL *targetURL = [NSURL fileURLWithPath:[path objectAtIndex:2]];
        NSLog(@"targeUrl--%@",targetURL);

        NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
        NSLog(@"request--%@",request);

        [webView loadRequest:request];
        [self.view addSubview:webView];
//        [webView release];

    }

}

-(IBAction)favbtnClicked:(id)sender
{
  NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSData *datapdf=[[NSData alloc]init];
    NSString *documentDirectory=[paths objectAtIndex:0];



    NSString *finalPath=[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"Favourite/%@",webView]];
    NSLog(@"finalpath--%@",finalPath);
    [datapdf writeToFile:finalPath atomically:YES];
    NSLog(@"data--%@",datapdf);

}

sel_id is return the cell of raw selected in tableview and I have make the Favourite folder in document directory and I want to add pdf file in Favourite folder. for that purpose I have created method of favbtnClicked: it is I have save the pdf file in document directory.

but in save like this formate...

Favourite folder >

but I want to save only pdf file like abc.pdf not this formate >

so give any suggestion and source code which is apply in my apps....

Upvotes: 1

Views: 7769

Answers (1)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

The problem is datapdf had nothing to write .So your favbtnClicked method would be like this:

 -(IBAction)favbtnClicked:(id)sender
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory=[paths objectAtIndex:0];

    NSString *finalPath=[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"Favourite/myFile.pdf"]]; //check your path correctly and provide your name dynamically
    NSLog(@"finalpath--%@",finalPath);
    NSData *datapdf = [NSData dataWithContentsOfURL:[NSURL urlWithString:yourURLString]]; //add url string here
    NSLog(@"data--%@",datapdf);
    if(datapdf)
       [datapdf writeToFile:finalPath atomically:YES];

}

Upvotes: 6

Related Questions