1

我是 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 中选择的原始单元格,我在文档目录中创建了收藏夹文件夹,我想在收藏夹文件夹中添加 pdf 文件。为此,我创建了 favbtnClicked 方法:我已将 pdf 文件保存在文档目录中。

但是像这样的甲型保存...

收藏夹 >

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

所以给出适用于我的应用程序的任何建议和源代码......

4

1 回答 1

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

}
于 2012-10-18T06:57:32.320 回答