I have to download a pdf from url on a button click in Objective-C
This is what I have done:
- (IBAction)DownLoadPdfButton:(UIButton *)sender {
NSString *currentURL = @"http://africau.edu/images/default/sample.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
[data writeToFile:filePath atomically:YES];
});
}
I don't want to declare any path for saving my pdf.. please provide answer only for downloading pdf on button click and if pdf is not downloading then show error on console.
I have refered other answers.. but they are not useful.