How to send a PDF file using UIActivityViewController

前端 未结 8 969
野趣味
野趣味 2020-12-13 13:57

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 s

相关标签:
8条回答
  • 2020-12-13 14:32

    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)
    }
    
    0 讨论(0)
  • 2020-12-13 14:32

    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)
    
    0 讨论(0)
  • 2020-12-13 14:40

    Above listing about Swift is deprecated in Swift 3

    let url = NSURL.fileURL(withPath: fileName)
    
    let activityViewController = UIActivityViewController(activityItems: [url] , applicationActivities: nil)
    
    present(activityViewController,
        animated: true,
        completion: nil)
    
    0 讨论(0)
  • 2020-12-13 14:42

    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];
    
    0 讨论(0)
  • 2020-12-13 14:46

    //In Swift

        let url = NSURL.fileURLWithPath(fileName)
    
        let activityViewController = UIActivityViewController(activityItems: [url] , applicationActivities: nil)
    
        presentViewController(activityViewController,
            animated: true,
            completion: nil)
    
    0 讨论(0)
  • 2020-12-13 14:48

    For Objective-C tested code to share PDF

    - (void)downloadPDFfile:(NSString *)fileName withFileURL:(NSString *)shareURL {
        dispatch_async(dispatch_get_main_queue(), ^ {
            NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *filePath = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",[self generateName:fileName withFiletype:@"pdf"]]];
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:shareURL]];
            NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                                    completionHandler:
                                          ^(NSData *data, NSURLResponse *response, NSError *error) {
                                              if (error) {
                                                  NSLog(@"Download Error:%@",error.description);
                                              } else if (data && error == nil) {
                                                  dispatch_async(dispatch_get_main_queue(), ^{
                                                      [data writeToFile:filePath atomically:YES];
                                                      [self shareFile:fileName withFilepath:filePath];
                                                  });
                                              }
                                          }];
    
            [task resume];
        });
    }
    
    -(void)shareFile:(NSString*)withfileName withFilepath:(NSString*)filePath {
        NSMutableArray *items = [NSMutableArray array];
    
        if (filePath) {
            [items addObject:[NSURL fileURLWithPath:filePath]];
        }
    
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
        [activityViewController setValue:withfileName forKey:@"subject"];
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            activityViewController.modalPresentationStyle = UIModalPresentationPopover;
            UIPopoverPresentationController *popPC = activityViewController.popoverPresentationController;
            popPC.sourceView = self.view;
            CGRect sourceRext = CGRectZero;
            sourceRext.origin = CGPointMake(self.view.frame.size.width-30, 0);
            popPC.sourceRect = sourceRext;
            popPC.permittedArrowDirections = UIPopoverArrowDirectionDown;
        }
    
        [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;
    }
    

    call function like below

    [self downloadPDFfile:@"yourFileName" withFileURL:shareURL];
    
    0 讨论(0)
提交回复
热议问题