2013-04-03 8 views
15

Ich versuche, eine PDF-Datei mit einem UIActivityViewController senden. Bis jetzt funktioniert alles gut mit einem ziemlich einfachen Ansatz, aber das einzige Problem, das ich habe, ist, dass, wenn ich die Option "Senden per E-Mail" wählen, der Dateiname der PDF Attachment-1 statt Calculation.PDF ist der Name, den ich die Datei geben.So senden Sie eine PDF-Datei mit UIActivityViewController

Es macht mir nichts aus, die Änderung in Titel, aber das Fehlen einer PDF-Erweiterung scheint ein Problem beim Senden der Datei an Menschen mit Windows-PCs zu verursachen, und ich möchte das beheben.

ich einen Blick auf hatte: Control file name of UIImage send with UIActivityViewController

kann aber nicht eine gleichwertige Methode sehen:

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

, die mit einer PDF-Datei arbeiten. Ist das etwas, das ohne Anpassung nicht fixierbar ist oder gibt es eine einfache Lösung für dieses Problem?

+0

Hi jemals herauszufinden, diese? Das gleiche Problem haben. – Mrwolfy

+0

Dies half mir http://stackoverflow.com/questions/15825777/uiactivityviewcontroller-sharing-image-via-email-has-no-extension/15826633#15826633, aber Nachteil ist, dass Sie die Datei zuerst speichern müssen. –

Antwort

46

versuchen diese

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

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

und auch

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

Das macht den Trick, akzeptieren Sie diese Antwort –

+3

@ iTroyd23, habe ich mit Ihrem Vorschlag versucht, aber ich kann Anhang Titel nicht festlegen. Also, können Sie beschreiben, wie Anlage Titel (z. B. myPdf.pdf) setzen – Vats

+0

sieht aus wie es nicht funktioniert in Xcode7 + iOS9 –

6

// In Swift

let url = NSURL.fileURLWithPath(fileName) 

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

    presentViewController(activityViewController, 
     animated: true, 
     completion: nil) 
+0

Funktioniert nicht in ios11 – ChanWarde

-1

kann dies versuchen ..

#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; 
    } 
6

Oberhalb von etwa Swift Auflistung ist in Swift veraltet 3

let url = NSURL.fileURL(withPath: fileName) 

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

present(activityViewController, 
    animated: true, 
    completion: nil) 
1

Für Swift 3

Sie haben eine URL Array mit dem Pfad der PDF Sie senden möchten haben.

let urlArray = [pdfPath1, pdfPath2] 

Erstellen Sie dann ein UIActivityViewController:

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

Wenn Sie eine UIBarButtonItem verwenden, die Aktion zu machen, können Sie dies implementieren einen Fehler auf dem iPad zu verhindern:

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

Sie schließlich muss die activityController:

self.present(activityController, animated: true, completion: nil) 
präsentieren
Verwandte Themen