Please refer to the following way to achieve this. Preserve file name of attached files from read from URL:
dispatch_async(dispatch_get_main_queue(), ^ {
NSData *fileData;
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentDir stringByAppendingPathComponent:@"filename.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.com/filename.pdf"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Download Error:%@",error.description);
}
if (data) {
fileData = data;
[data writeToFile:filePath atomically:YES];
NSLog(@"File is saved to %@",filePath);
}
}];
if(fileData && filePath) {
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[NSURL fileURLWithPath:filePath]] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
}
});