Reputation: 151
I have a code that downloads a file and store it in the documents folder:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:file.url]];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, file.name];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation start];
When the request is completed, I want to show that same file on a UIWebView using this code:
[operation setCompletionBlock:^{
dispatch_sync(dispatch_get_main_queue(), ^{
UIWebView* webView = [[UIWebView alloc] init];
NSURL *targetURL = [[NSBundle mainBundle] URLForResource:file.nameWithoutExtension withExtension:@"pdf"];
webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
ViewerVC* viewer = [[ViewerVC alloc] init];
[viewer.view addSubview:webView];
[self.navigationController pushViewController:viewer animated:FALSE];
});
});
But it does not work, the URL does not find the file I just downloaded, Am i saving or searching it wrong?
(The code is slightly different from my actual code for understanding purposes, but the idea is the same. I even tried this example with some hard coded URL, like: http://www.selab.isti.cnr.it/ws-mate/example.pdf)
Upvotes: 2
Views: 1394
Reputation: 4244
[[NSBundle mainBundle] URLForResource:file.nameWithoutExtension withExtension:@"pdf"]
will give you file from your xcode project folder.
Use this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"yourFileName.pdf"];
It will get you a file from Documents folder.
Upvotes: 2