4

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"];
2
  • 1
    yes I think it is possible, try this How to Answer[1] [1]: stackoverflow.com/questions/13250463/… Commented Jul 14, 2013 at 15:22
  • yes that works fine. Don't know why I didn't fry that before. Thanks. Please copy the comment to an answer so I can accept it. Commented Jul 15, 2013 at 8:48

2 Answers 2

6

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];
Sign up to request clarification or add additional context in comments.

2 Comments

is there anyway to change the file name?
@MobileMon Hi, try to save file with needed name on the disk and set it instead "AppDistributionGuide". Hope it will help you.
0

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];
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.