PDA

View Full Version : download and retrieve pdf with asihttprequest




H34db4ng3r
Aug 31, 2012, 05:09 AM
hello everybody :) ,

I'm trying to download a pdf via url into my iPad cache file system and retrieve it, to show it in an uiwebview. i want to use asihttp but also choose alternatives as long as it works ;) i have 2 buttons in a scrollview: one to download and the second to display the pdf. I'm a beginner in obj c. so i need a bit help:

download function: NSURL *url = [NSURL URLWithString:@"urlForPDF"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved pdf location
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"data.pdf"];


NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your temp pdf location
NSString *tempPdfLocation = [cachesDirectory stringByAppendingPathComponent:@"data.pdf"];

[request setTemporaryFileDownloadPath:tempPdfLocation];
[request setDownloadDestinationPath:pdfLocation];
[request startAsynchronous];
}



the activatey indicator is moving, but i can't see a file in my cache folder on simulator. do i miss something?

the display method:

- (IBAction)display:(id)sender {


UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];


// init and create the UIWebView
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

//set the web view delegates for the web view to be itself
[webView setDelegate:self];

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:@"data.pdf"];

// Now tell your UIWebView to load that file
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:pdfLocation]]];


}



when i click on the display button, nothing happens.

can somebody help me? have searched a lot on stack overflow but haunt found a solution yet.



ArtOfWarfare
Aug 31, 2012, 08:57 AM
Hello, a few things:

1 - When you use non standard libraries, tell a little about them. You didn't give so much as a link as to where it came from. I was really confused why it wasn't in the documentation until I googled for it.

2 - Copy and paste your actual code. The issue could be in the first line, the NSURL line, and we wouldn't be able to help you becuase your string is @"urlForPDF" which isn't a valid URL, but I suspect you already knew that. Unless you think that you're accessing a variable, which you're not, because that's a string literal.

Now onto some actual help... you need to either set a delegate for your request, or use blocks. How do I know? Because the documentation for the class says so. Did you look here:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#creating_an_asynchronous_request

It's near the very top of the documentation.

The first code example uses delegates (you'll need to add <ASIHTTPReqestDelegate> to whichever class declaration you want to serve as the delegate.)

The second code example uses blocks instead.

Personally, I'd use delegates just because they're more backwards compatible. Blocks have only existed since iOS 4, which means, according to my handy dandy device support matrix:
https://www.dropbox.com/s/u4lp189clie04ki/iOS_Support_Matrix_AUTUMN2012-2.pdf
... it won't run on the original iPhone or iPod Touch. Also, it only supports ~86% of devices. I guess blocks really aren't new anymore... it's probably safe to just use blocks and not care about the devices that don't support them.

So maybe just use blocks. The choice is yours.

Either way, you need to have some means of determining that your download method is actually working. Right now you have none. There are three possibilities for why things aren't working for you:

1 - You're trying to open your PDF before it finishes downloading.
2 - You're trying to open a PDF that failed to download.
3 - Your PDF successfully downloaded but your display method doesn't work.

You have no idea which of the three it is right now, because you give yourself no means of figuring out whether the download finished successfully, failed to download, or hasn't finished downloading yet. Simple NSLog(@"Download successful.");
NSLog(@"Download failed.") statements would work.

Let us know what you find after doing this.

PhoneyDeveloper
Aug 31, 2012, 08:25 PM
You should print out the paths your code generates so you can see for sure where the file is going in your file system. When doing this sort of thing it is usual to start the web view loading when the file finishes downloading. You can only do this in the appropriate delegate method.

Oh, you need to add your web view as a subview to an existing view or you can't see it.

H34db4ng3r
Sep 4, 2012, 01:10 AM
ok thx so far: i just tried it with these methods

-(void) download

{

[NSURL *url = [NSURL URLWithString:@"http://www.pbs-software.com/White-Paper.140.0.html?&eID=tx_pbspdb_file&file=526&lid=0&token=e7ad57f1"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

}


- (void)requestFinished: (ASIHTTPRequest *)request

{

NSData *responseData = [request responseData];

}


I've put an break point in the "- (void)requestFinished:(ASIHTTPRequest *)request" method after pressing the download button, the app is stopped. so obv. the method is called and the download successful.
but i don't real understand how the data is stored. I got maybe 40 download buttons in miy app, so every button is calling the "- (void)requestFinished:(ASIHTTPRequest *)request" method and creates a nsdata object. but how do i retrieve them correctly ?

H34db4ng3r
Sep 4, 2012, 03:56 AM
my display method has to be like this or? :



- (IBAction)display:(id)sender {

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
webView.scalesPageToFit = YES;
[webView addSubview:webView];


NSURL *pdfURL = [NSURL fileURLWithPath:<#(NSString *)#>];

NSURLRequest *request = [NSURLRequest requestWithURL:pdfURL];
[webView loadRequest:request];



but how do i get the nsstring for my nsurl in order to create the nsurlrequest?

PhoneyDeveloper
Sep 4, 2012, 06:02 PM
You need to tell ASIHTTPRequest the file to which it should download. You can set the userData on the request to be this path so you can identify it when the request finishes.

H34db4ng3r
Sep 7, 2012, 06:14 AM
ok that sounds legit. but how do i know the right path for storing the data "permanently" and how do i implement that correctly in coding? asihttprequest site doesn't show me a way to do that

dejo
Sep 7, 2012, 08:53 AM
but how do i know the right path for storing the data "permanently" and how do i implement that correctly in coding? asihttprequest site doesn't show me a way to do that

That up to you to code. ASIHTTPRequest has nothing to do with saving a file locally.