0
votes

I am using AFNetworking to download a pdf (that will change on a weekly basis) and save it into the documents directory with this code:

//Get the PDF    

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.somewebsiteaddress/CurrentEdition1.pdf"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:@"CurrentEdition1.pdf"];

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", filePath);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation start];

I then want to read that saved file in another section of the app in a UIWebView (after it has downloaded) and use this code:

//Now create Request for the file that was saved in your documents folder

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"CurrentEdition1.pdf"];

NSURL *url = [NSURL fileURLWithPath:filePath];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webVieweedition setUserInteractionEnabled:YES];

[webVieweedition setDelegate:self];

[webVieweedition loadRequest:requestObj];

I have used a few single page pdf documents to test with - loading them up on the server and then seeing if they are downloaded and then viewed when changed. Problem is, this seems to only be working about 50% of the time. I'll put a new file up and sometimes the correct one will be shown in the UIWebView and sometimes it will show the previous one and not the new one. I am waiting until I see the download completed message before I try to go to the UIWebView (although I know clients won't do that, but that's a whole other question). Anyway, I'm new to XCode and have just been a web html guy. This has had my head spinning for two days. Using Storyboards, ARC, XCode 4.6.2.

1

1 Answers

1
votes

If i understood right, sometimes you see the same pdfs in app, although you changed them on webserver?May be the reason is cache, try construct request this way, ignoring caching

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.somewebsiteaddress/CurrentEdition1.pdf"]  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];