Home » Swift » How to send a PDF file using UIActivityViewController

How to send a PDF file using UIActivityViewController

Posted by: admin November 30, 2017 Leave a comment


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?


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


//In Swift

    let url = NSURL.fileURLWithPath(fileName)

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

        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)


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)


May be try this..

    #define IS_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

    // Validate PDF using NSData
    - (BOOL)isValidePDF:(NSData *)pdfData {
        BOOL isPDF = false;
        if (pdfData.length >= 1024 ) {

            int startMetaCount = 4, endMetaCount = 5;
            // check pdf data is the NSData with embedded %PDF & %%EOF
            NSData *startPDFData = [NSData dataWithBytes:"%PDF" length:startMetaCount];
            NSData *endPDFData = [NSData dataWithBytes:"%%EOF" length:endMetaCount];
            // startPDFData, endPDFData data are the NSData with embedded in pdfData
            NSRange startRange = [pdfData rangeOfData:startPDFData options:0 range:NSMakeRange(0, 1024)];
            NSRange endRange = [pdfData rangeOfData:endPDFData options:0 range:NSMakeRange(0, pdfData.length)];

            if (startRange.location != NSNotFound && startRange.length == startMetaCount && endRange.location != NSNotFound && endRange.length == endMetaCount ) {
                // This assumes the checkstartPDFData doesn't have a specific range in file pdf data
                isPDF = true;

            } else  {
                isPDF = false;
        return isPDF;

    // Download PDF file in asynchronous way and validate pdf file formate.
    - (void)downloadPDFfile:(NSString *) fileName withFileURL:(NSString *) url {
        NSString *filePath = @"";
         dispatch_async(dispatch_get_main_queue(), ^ {
            NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            filePath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/Attachments/%@",[self generateName:fileName withFiletype:@"pdf"]]];
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                if (error) {
                        NSLog(@"Download Error:%@",error.description);
                } else if (data && error == nil) {
                    bool checkPdfFormat = [self isValidePDF:data];
                    if (checkPdfFormat) {
                        //saving is done on main thread
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [data writeToFile:filePath atomically:YES];
                            NSURL *url = [NSURL fileURLWithPath:filePath];
                            [self triggerShare:fileName withFilepath:filePath];

    // Trigger default share and print functionality using UIActivityViewController
    -(void) triggerShare:(NSString*)fileName withFilepath:(NSString*)filePath {
            * Set this available field on the activity controller */
            NSMutableArray *items = [NSMutableArray array];

            if (filePath) {
                [items addObject:[NSURL fileURLWithPath:filePath]];

            UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
            [activityViewController setValue:fileName forKey:@"subject"]; // Set the mail subject.

            if (IS_IPAD) {
                activityViewController.modalPresentationStyle = UIModalPresentationPopover;
                UIPopoverPresentationController *popPC = activityViewController.popoverPresentationController;
                popPC.sourceView = self.view;
                CGRect sourceRext = CGRectZero;
                sourceRext.origin = CGPointMake(self.view.frame.size.width-30, 0 ); // I have share button in navigation bar. ;)
                popPC.sourceRect = sourceRext;
                popPC.permittedArrowDirections = UIPopoverArrowDirectionUp;

            [activityViewController setCompletionWithItemsHandler:
             ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
            [self presentViewController:activityViewController animated:YES completion:nil];

    -(NSString*) generateName:(NSString*)title withFiletype:(NSString*)type {

        NSString *subject = [title stringByReplacingOccurrencesOfString:@" " withString:@"_"];
        subject = [NSString stringWithFormat:@"%@.%@",subject,type];
        return subject;