从服务器下载pdf文件,并将其保存在ApplicationSupport目录中。 iOS

| 我是iOS开发的新手,我想知道如何从服务器下载文件并将其保存在“应用程序支持”文件夹中。我想将其保存为.pdf文件,以便能够在UIWebView中显示它。 在不同的网站上呆了很长时间之后,我认为我应该使用NSURLConnection(异步)来下载它。或NSData(我已经尝试过了,但是没有用)。 那么,有人可以通过显示示例代码来帮助我吗? 非常感谢 :)     
已邀请:
        看看这个S.O.有关如何执行此操作的示例的问题。 本示例使用ASIHTTPRequest,它是
NSURLRequest
NSURLConnection
的替代方法。我强烈建议您使用此框架,这将使您的生活更加轻松。 如果您真的愿意使用
NSURLRequest
NSURLConnection
,请参阅其他主题。     
        
[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];
         }
     }
 }];
    

要回复问题请先登录注册