objective-c - delegate - uiactivityviewcontroller title

How to send a PDF file using UIActivityViewController (6)

Swift 4.0

Here I attached code.I just added thread handling in to present "activityViewController" because of this viewcontroller present before load actual data.

let url = NSURLfileURL(withPath:fileName)

let activityViewController = UIActivityViewController(activityItems: [url] , applicationActivities: nil)

DispatchQueue.main.async {

    self.present(activityViewController, animated: true, completion: nil)

I'm trying to send a PDF using a UIActivityViewController. So far everything works fine using a fairly basic approach but the one issue I have is that when I select the send by mail option, the PDF's file name is Attachment-1 rather than Calculation.PDF which is the name that I give the file.

I don't mind too much the change in title, but the lack of a .pdf extension does seem to cause a problem when sending the file to people with Windows PC's and I'd like to fix that.

I've had a look at: Control file name of UIImage send with UIActivityViewController

But can't see an equivalent method to:

[mailComposer addAttachmentData: UIImagePNGRepresentation(viewImage) mimeType:@"" fileName:@"myImage.png"];

that will work with a PDF file. Is this something that is not fixable without customization or is there a simple solution to this problem?

//In Swift

    let url = NSURL.fileURLWithPath(fileName)

    let activityViewController = UIActivityViewController(activityItems: [url] , applicationActivities: nil)

        animated: true,
        completion: nil)

For Swift 3

You have to have a URL array with the path of the PDF you want to send.

let urlArray = [pdfPath1, pdfPath2]

Then create an UIActivityViewController:

let activityController = UIActivityViewController(activityItems: urlArray, applicationActivities: nil)

If you are using a UIBarButtonItem to make that action, you can implement this to prevent an error on iPad:

if let popover = activityController.popoverPresentationController {
   popover.barButtonItem = self.barButtonItem

Finally you have to present the activityController:

self.present(activityController, animated: true, completion: nil)

Above listing about Swift is deprecated in Swift 3

let url = NSURL.fileURL(withPath: fileName)

let activityViewController = UIActivityViewController(activityItems: [url] , applicationActivities: nil)

    animated: true,
    completion: nil)

The reply by Muruganandham K is simple and quite elegant. However, it doesn't work in iOS 9. In order to make it work, if you remove the @[@"Test" and just pass the pdfData, an attachment is made.

NSData *pdfData = [NSData dataWithContentsOfFile:pdfFilePath];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:pdfData applicationActivities:nil];

[self presentViewController:activityViewController animated:YES completion:nil];

try this

NSData *pdfData = [NSData dataWithContentsOfFile:pdfFilePath];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", pdfData] applicationActivities:nil];

[self presentViewController:activityViewController animated:YES completion:nil];

and also

NSString *str = [[NSBundle mainBundle] pathForResource:@"AppDistributionGuide" ofType:@"pdf"];   
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", [NSURL fileURLWithPath:str]] applicationActivities:nil];