2016-04-05 5 views
1

私のアプリにボタンがあります。私が開くと、attachemntの新しいメールメッセージが表示されます。電子メールアドレスに電子メールを送信した後、このメッセージと添付ファイルがありますが、添付ファイルにはアプリケーションの最後の画面が含まれています。このファイルをサポートファイルから添付して開きます。たぶんあなたはどこでミスをすることができますか?アプリiOSに添付ファイル付きのメールを送信するにはどうすればいいですか?

- (IBAction)showEmail:(id)sender { 

NSString *emailTitle = @"elllo"; 

NSString *messageBody = @"Hi ! \n Below I send you "; 

NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"]; 

NSMutableData *pdfData = [NSMutableData data]; 
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil); 
UIGraphicsBeginPDFPage(); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIGraphicsEndPDFContext(); 
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 

mc.mailComposeDelegate = self; 

[mc setSubject:emailTitle]; 
[mc setMessageBody:messageBody isHTML:NO]; 
[mc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"MV.pdf"]; 
[mc setToRecipients:toRecipents]; 

[self presentViewController:mc animated:YES completion:NULL]; 

}

+1

。スクリーンショットを作成していることを意味し、実際のP​​DF自体を描画します。 – ogres

+0

ありがとうございました!サポートファイルから添付ファイル(pdfファイル)付きのメールを送信するにはどうすればよいですか? – mechu911

+0

最初にファイルをNSDataとして読み込み、そのデータを添付することができます – ogres

答えて

6

それは正しいsollutionだ下記[renderInContext self.view.layer:UIGraphicsGetCurrentContext()]

- (IBAction)showEmail:(id)sender { 

    NSString *emailTitle = @"elllo"; 

    NSString *messageBody = @"Hi ! \n Below I send you "; 

    NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MV" ofType:@"pdf"]; NSData *myData 
= [NSData dataWithContentsOfFile: path]; 

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 

    mc.mailComposeDelegate = self; 
    [mc setSubject:emailTitle]; 
    [mc setMessageBody:messageBody isHTML:NO]; 
    [mc addAttachmentData:myData mimeType:@"application/pdf" fileName:@"MV.pdf"]; 

    [mc setToRecipients:toRecipents]; 

    [self presentViewController:mc animated:YES completion:NULL]; 

} 
+0

偉大な答え!これだよ! –

関連する問題