2011-04-05 84 views
0
 -(IBAction)click; 
    { 

      NSURL *url = [NSURL URLWithString:URL_TO_DOWNLOAD]; 
      NSString *tempDownloadPath = [[self documentsDirectory] 
              stringByAppendingPathComponent:@"test.pdf"]; 
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
      [request setDownloadDestinationPath:[self documentsDirectory]]; 
      [request setTemporaryFileDownloadPath:tempDownloadPath]; 
      [request setDelegate:self]; 

    [request startAsynchronous]; 


     } 

    - (void)requestFinished:(ASIHTTPRequest *)request 
     { 
      NSLog(@"requestFinished"); 
      NSError *error; 
      NSFileManager *fileManager = [[NSFileManager alloc] init]; 
      NSLog(@"Documents directory: %@", [fileManager 
               contentsOfDirectoryAtPath:[self 
                      documentsDirectory] error:&error]); 
      NSString *dir = [self documentsDirectory]; 
      NSLog(dir); 
      // NSData *responseData = [request responseData]; 
      NSArray *array = [[NSFileManager defaultManager] 
           contentsOfDirectoryAtPath:dir error:&error]; 
      if (array == nil) { 
       NSLog(@"array == nil"); 
      } 
else 
{ 
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:tempDownloadPath]]]; 
     [[self view] addSubview:aWebView]; 
} 

      [fileManager release]; 
       } 

    - (NSString *)documentsDirectory { 
      NSArray *paths = 
      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
               NSUserDomainMask, 
               YES); 
      return [paths objectAtIndex:0]; 
     } 

我無法管理檢查文件是否存在,然後再點擊下載,或者實際顯示PDF請求已完成。任何解決方案?iphone pdf下載

回答

2

要檢查文件是否存在,用途:

BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:file] 

要顯示PDF一旦請求完成後,您可以在UIWebView打開它,或者使用CGPDF*組函數來呈現它。

ASIHTTPRequest的setDownloadDestinationPath預計會收到一個絕對文件路徑,而您似乎只是傳遞文檔目錄。 你可以從你的URL中的文件名,並追加到文件目錄路徑:

NSString *filename = [[url absoluteString] lastPathComponent]; 

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]; 
NSString *destPath = [directory stringByAppendingPathComponent:filename]; 

[request setDownloadDestinationPath:destPath]; 

然後檢查,如果下載的文件確實存在,可以再次使用destPath

0

check this out,我做了它的圖像,但你可以很容易地修改它做PDF。我希望它有幫助。

+0

我試圖修改文件來支持PDF,但是,它實際上不會通過簡單地更改文件擴展名來加載PDF .. – 2011-04-05 14:02:00

+0

您需要使用與呈現PDF相同的方式來呈現它,查看Apple的Quartz示例 – 2011-04-07 12:06:45