【问题标题】:Download pdf file from server, save it in ApplicationSupport directory. iOS从服务器下载 pdf 文件,保存在 ApplicationSupport 目录中。 iOS
【发布时间】:2011-09-14 11:58:49
【问题描述】:

我是 iOS 开发的新手,我想知道如何从服务器下载文件,并将其保存在我的 Application Support 文件夹中。我想将它保存为 .pdf 文件,以便能够在 UIWebView 中显示它。

在不同的网站呆了很长时间,我想我应该使用 NSURLConnection (异步)来下载它。或者 NSData (我已经试过了,但没用)。

那么,有人可以帮我看看这个示例代码吗?

非常感谢:)

【问题讨论】:

  • 您下载的文件的原始格式是什么?是pdf的吗?
  • 是的!并且该文件存在于服务器上;)

标签: ios pdf download nsurlconnection


【解决方案1】:
[self.productListArray enumerateObjectsUsingBlock:^(NSDictionary *productDictionary, NSUInteger idx, BOOL *stop)
{

     NSFileManager *fileManger=[NSFileManager defaultManager];

     if(![fileManger fileExistsAtPath:pdfString])
     {
         dispatch_async(serialQueue, ^()
                        {
                            NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];

                            NSURLResponse *response = nil;
                            NSError *connectionError = nil;

                            NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                                 returningResponse:&response
                                                                             error:&connectionError];

                            if(connectionError)
                            {
                                NSLog(@"Pdf Connection Error==>%@",connectionError.userInfo);
                                [AMSharedClass showAlertMessge:@"Request timeout"];

                            }
                            else if ([response.MIMEType isEqualToString:@"application/pdf"])
                            {
                                NSLog(@"pdfFilePathURLString==>%@",pdfString);

                                [data writeToFile:pdfString atomically:YES];
                            }
                            else
                            {
                                [AMSharedClass showAlertMessge:@"Pdf not found."];
                                if (idx+1 == [self.productListArray count])
                                {
                                    [self.btnSetting setEnabled:NO];
                                }
                            }
                            if (idx+1 == [self.productListArray count])
                            {
                                [[[AMSharedClass object]sharedHUD]hideOnWindow];
                                self.pdfURLString =  [self joinPDF:self.productFilePathUrlArray WithDetails:self.pdfInfoArray];
                                [self initialConfiguration];
                                NSLog(@"%@",self.productFilePathUrlArray);
                            }
                        });

         // Long running task

     }
     else
     {
         if (idx+1 == [self.productListArray count])
         {
             self.pdfURLString =  [self joinPDF:self.productFilePathUrlArray WithDetails:self.pdfInfoArray];
             [self initialConfiguration];
             NSLog(@"%@",self.productFilePathUrlArray);
             [[[AMSharedClass object]sharedHUD]hideOnWindow];
         }
     }
 }];

【讨论】:

    【解决方案2】:

    查看S.O. question 以了解如何操作的示例。

    此示例使用ASIHTTPRequest,它是NSURLRequestNSURLConnection 的替代方案。我强烈建议你使用这个框架,它会让你的生活更轻松。

    如果您真的愿意使用NSURLRequestNSURLConnection,请参阅this other topic

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多