如何在文档目录中保存pdf文件

时间:2012-10-18 06:28:50

标签: objective-c

我是iPhone的初学者。我不知道如何在文档目录中保存pdf文件。我在文档目录中创建了该文件夹。我首先在Webview中打开pdf。但是如何在文档目录中存储pdf。

这是我的代码......

- (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返回tableview中选择的raw单元格,我在文档目录中创建了Favorite文件夹,我想在Favorite文件夹中添加pdf文件。为此我创建了favbtnClicked的方法:我将pdf文件保存在文档目录中。

但是像这样的格式化......

收藏夹      >

但我想只保存像abc.pdf这样的pdf文件,而不是这个格式>

请提供适用于我的应用程序的任何建议和源代码....

1 个答案:

答案 0 :(得分:6)

问题是 datapdf无需编写。因此,您的favbtnClicked方法将是这样的:

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

}