Reputation: 5122
Is there an equivalent code to this (code below) for iOS6 UIActivityViewController? The question is, can I share data via url and preserve the file name? As of now the only way I can figure out how to do it is to convert the PDF to NSData and add the data as an activityItem. Works fine, but I lose the name of the attached pdf.
[composer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"myPDF.pdf"];
Upvotes: 4
Views: 7560
Reputation: 598
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];
}
});
Upvotes: 0
Reputation: 1378
yes I think it is possible, try this
NSString *str = [[NSBundle mainBundle] pathForResource:@"AppDistributionGuide" ofType:@"pdf"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", [NSURL fileURLWithPath:str]] applicationActivities:nil];
Upvotes: 6