Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在Xcode5中将PDF文件附加到电子邮件_Objective C_Ipad_Email_Ios7.1 - Fatal编程技术网

Objective c 如何在Xcode5中将PDF文件附加到电子邮件

Objective c 如何在Xcode5中将PDF文件附加到电子邮件,objective-c,ipad,email,ios7.1,Objective C,Ipad,Email,Ios7.1,我正在Xcode5中为Ipad(iOS7.1)开发一个应用程序,它打开一个电子邮件模式视图控制器,使用MFMailComposeViewController发送电子邮件 我想在我的电子邮件中附加一个PDF存档 这是我的代码: 职能.m: #import <MessageUI/MessageUI.h> @interface Functions()<MFMailComposeViewControllerDelegate> @end @implementation Fun

我正在Xcode5中为Ipad(iOS7.1)开发一个应用程序,它打开一个电子邮件模式视图控制器,使用MFMailComposeViewController发送电子邮件

我想在我的电子邮件中附加一个PDF存档 这是我的代码:

职能.m:

#import <MessageUI/MessageUI.h>

@interface Functions()<MFMailComposeViewControllerDelegate>

@end

@implementation Functions

-(void)sendEmailInViewController:(UIViewController *)viewController {

    NSString *emailTitle = @"Hello";
    NSArray *toRecipents = [[NSArray alloc]initWithObjects:@"jesus@mymail.com", nil];
    NSMutableString *messageBody =[[NSMutableString alloc]init];

    [messageBody appendString:@"<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><span style=\"font-size:14px;\"><span style=\"color: rgb(0, 0, 102);\"><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Jesus</strong><br />Gerente Select&nbsp;&nbsp; / Sucursal&nbsp;&nbsp;&nbsp;Santa Fe<br />Paseo de la Lidia No. 801&nbsp;Piso 8 Mod. 162<br />Col. Lomas del Pedregal, C.P. 01292, M&eacute;xico D.F.<br />Tel: + (55) 8728-0908. Ext. 12832<br />e-mail:&nbsp; jesus@mymail.com</span></span></span></p><p><span style=\"color:#000066;\"><span style=\"font-family: arial,helvetica,sans-serif;\"></span></span></p>"];



    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil) {
        MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
        mailView.mailComposeDelegate = self;

        //Set the subject
        [mailView setSubject:emailTitle];

        //Set the mail body
        [mailView setMessageBody:messageBody isHTML:YES];
        [mailView setToRecipients:toRecipents];



        NSData *pdfData = [NSData dataWithContentsOfFile:@"google.pdf"];
        [mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google.pdf"];


        //Display Email Composer
        if([mailClass canSendMail]) {
            [viewController presentViewController:mailView animated:YES completion:NULL];
        }
    }
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result){
        case MFMailComposeResultCancelled:NSLog(@"Mail cancelled"); break;
        case MFMailComposeResultSaved:    NSLog(@"Mail saved");     break;
        case MFMailComposeResultSent:     NSLog(@"Mail sent");      break;
        case MFMailComposeResultFailed:   NSLog(@"Mail sent failure: %@", [error localizedDescription]);                                 break;
        default:                                                    break;
    }

    // Close the Mail Interface
    if (!app) { app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; }
    if (!currentSplitViewController) {
        currentSplitViewController  = (UISplitViewController *) app.window.rootViewController;
    }

    navController        = [currentSplitViewController.viewControllers lastObject];

    [[navController topViewController] dismissViewControllerAnimated:YES completion:NULL];

}

@end
这是我的邮件截图:

它显示了我的pdf文件的徽标,但当我的邮件被发送时,我打开了我的收件箱,但我只找到了签名,但任何附加的内容都可以查看。。。如何正确地附加和发送它

任何帮助我都会感激的


编辑:这是答案:我刚刚不得不添加这个

NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"google" ofType:@"pdf"]];
        [mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google"];
这是我的结果:


听起来您需要了解MFMailComposeViewController的方法

使用这种方法,您将能够为要发送的任何存档添加原始NSData。只需确保您设置了适当的mime类型(例如“
application/zip
”)和文件名(例如“MyArchive.zip”)


对于PDF文件,您将使用类似“google.PDF”的文件名。然后,您只需加载一个NSData对象,其中包含要附加的文件数据。

听起来您需要查看MFMailComposeViewController的方法

使用这种方法,您将能够为要发送的任何存档添加原始NSData。只需确保您设置了适当的mime类型(例如“
application/zip
”)和文件名(例如“MyArchive.zip”)


对于PDF文件,您将使用类似“google.PDF”的文件名。然后,您需要做的就是加载一个NSData对象,其中包含您要附加的文件数据。

我将这些行添加到电子邮件中,我在签名后看到一个小徽标,但当我发送它时,我看不到pdf附加的NSData*pdfData=[NSData data with CONTENTSOFILE:@“google.pdf]”;[mailView addAttachmentData:pdfData mimeType:@“application/pdf”文件名:@“google.pdf”];是的,那是我的pdf,是的,它在我的资源包中。我怀疑您的问题是“pdfData”为零,因为您没有指定“google.pdf”文件的正确位置。尝试使用“
NSData*pdfData=[NSData dataWithContentsOfURL:[[NSBundle mainBundle]resourceURL]URLByAppendingPathComponent:@“google.pdf”];
”,看看效果是否更好。如果不起作用,请在代码中设置一个断点,并确保数据对象不是零,并且包含的字节数超过零。我也有同样的问题,因为示例不适用于meI将这些行添加到我的电子邮件中,我在签名后看到一个小徽标,但当我发送它时,我看不到我的pdf附加NSData*pdfData=[NSData DATA WITH CONTENTS OFFILE:@“google.pdf”];[mailView addAttachmentData:pdfData mimeType:@“application/pdf”文件名:@“google.pdf”];是的,这是我的pdf,是在我的资源包中。我怀疑您的问题是“pdfData”为零,因为您没有指定“google.pdf”文件的正确位置。请尝试使用“
NSData*pdfData=[NSData dataWithContentsOfURL:[[[NSBundle mainBundle]resourceURL]URLByAppendingPathComponent:@“google.pdf”];
“然后看看效果是否更好。如果不起作用,请在代码中设置一个断点,并确保数据对象不是nil,并且包含的字节数超过零。我也有同样的问题,但该示例对我不起作用。”
NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"google" ofType:@"pdf"]];
        [mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google"];