如何在iphone应用程序中附加电子邮件中的任何文档
我从iphone应用程序发送电子邮件,它工作正常,但我希望通过电子邮件我应该附加一个pdf文件,这是应用程序的文档文件夹。首先,为了测试,我从应用程序的资源文件夹附加了一个png,但它没有附加,也没有在电子邮件中发送。我使用以下代码如何在iphone应用程序中附加电子邮件中的任何文档,iphone,objective-c,ipad,email,Iphone,Objective C,Ipad,Email,我从iphone应用程序发送电子邮件,它工作正常,但我希望通过电子邮件我应该附加一个pdf文件,这是应用程序的文档文件夹。首先,为了测试,我从应用程序的资源文件夹附加了一个png,但它没有附加,也没有在电子邮件中发送。我使用以下代码 - (IBAction)onEmailResult { if ([[MFMailComposeViewController class] canSendMail]) { MFMailComposeViewController *picker =
- (IBAction)onEmailResult
{
if ([[MFMailComposeViewController class] canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Pig Game"];
[picker setToRecipients:toRecipients];
int a=10;
int b=100;
NSString *path = [[NSBundle mainBundle] pathForResource:@"project existing photo" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"png" fileName:@"icon.png"];
NSString * emailBody = [NSString stringWithFormat:@"My Score %d",a];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
int a=10;
int b=20;
NSString *recipients = @"mailto:imran_husain_2000@yahoo.com?&subject=Pig Game";
NSString *body = [NSString stringWithFormat:@"&body=My Score: %d/%d, My Time: %@", a,b, time];
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
}
这段代码在DisplayMailComposer表单中实现,我希望这段代码对您有用MFMailComposeViewController*picker1=[[MFMailComposeViewController alloc]init];
MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
NSArray *recipentsArray = [[NSArray alloc]initWithObjects:[shopDictValues objectForKey:@"vEmail"], nil];
picker1.mailComposeDelegate = self;
picker1.modalPresentationStyle = UIModalPresentationFormSheet;
[picker1 setSubject:@"Subject"];
[picker1 setToRecipients:recipentsArray];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableArray *arydefault = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] valueForKey:@"jacketCount"]];
for (int i=0;i<arydefault.count;i++)
{
NSString *pdfFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.pdf",[[arydefault objectAtIndex:i]valueForKey:@"productlinename"],[arydefault objectAtIndex:i]]];
[picker1 addAttachmentData:[NSData dataWithContentsOfFile:pdfFilePath] mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"Order - %@",[[[NSString stringWithFormat:@"%@%i",[[arr objectAtIndex:i]valueForKey:@"productlinename"],i] componentsSeparatedByCharactersInSet: [[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""]]];
}
NSString *emailBody = @"Email Body goes here.";
[picker1 setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker1 animated:YES];
NSArray*RecipientSarray=[[NSArray alloc]initWithObjects:[shopDictValues objectForKey:@“vEmail”],nil];
picker1.mailComposeDelegate=self;
picker1.modalPresentationStyle=UIModalPresentationFormSheet;
[picker1设置主题:@“主题”];
[picker1设置为收件人:Recipientsarray];
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSMutableArray*arydefault=[[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]valueForKey:@“jacketCount”];
对于(inti=0;i请尝试以下代码段
- (NSString *)pathForFile : (NSString *) fileName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}
- (void)sendMailWithAttachedFile:(NSString *) fileName extention:(NSString *) extension{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:@"%@.%@", fileName, extension]]];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:@"application/pdf" fileName:@"TestOne.pdf"];
[self presentViewController:picker animated:YES completion:nil];
}
现在调用Send Mail方法,如下所示:
[self sendMailWithAttachedFile:@"TestOne" :@"pdf"];
这里我附上了PDF的编号。因此,我已经循环了它。productlinename这是我可以给出我的PDF名称的地方Yes这里[NSString stringWithFormat:@“%@.PDF”,yourFileName];为什么downvote如果我这边有任何东西正在工作,我已经编写了正确的代码,所以这并不意味着要downvote?MIMIE类型看起来不好。请尝试将其更改为“图像/png"。请尝试这两个链接。我猜您获得了否决票,因为您所问的问题可以通过运行搜索轻松回答。当然,他或她应该说明原因。@t蓝色我从我这边更改了mime类型,现在可以为您提供评论文件名EmailReport这是什么,在您的文件扩展名中提供什么代码我已在您的编辑中添加了我的代码,请查看它在电子邮件中显示TestOne.pdf,但未附加在电子邮件中我从应用程序的文档文件夹中获取此文件的文件名为TestOne,文件名为TestOne如何提供该路径找不到pdf标题:`%pdf'未找到。这会出现错误我想我需要发布另一个问题,说明如何从do附加文件在iphone应用程序中发送电子邮件的文档目录这是我的问题
[self sendMailWithAttachedFile:@"TestOne" :@"pdf"];