【问题标题】:ios sending docx attachments with mfMailComposer failsios 使用 mfMailComposer 发送 docx 附件失败
【发布时间】:2015-12-30 08:29:01
【问题描述】:

我在发送电子邮件时尝试附加 docx 文件,但遇到了问题。这两行始终为零。变量文件名和扩展名不是零,但文件出现为零。不确定这是否重要,但 docx 文件是动态生成的,如果我要使用生成文件中的 NSData 而不是在这里创建新的 NSData 变量怎么办?

    filepath = /Users/myself/components/file.docx

NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
    NSData *fileData

= [NSData dataWithContentsOfFile:file];

这里是代码

    -(void)sendEmail:(NSString*)filePath{
  if ([MFMailComposeViewController canSendMail])
  {
  mailComposer = [[MFMailComposeViewController alloc]init];
  mailComposer.mailComposeDelegate = self;
  [mailComposer setToRecipients:@[@"name@gmail.com"]];
  [mailComposer setSubject:@"Mobile File Attachment"];
  [mailComposer setMessageBody:@"DocX file attached" isHTML:NO];

    // Determine the file name and extension
    if(![filePath isEqual:nil])
    {
    NSArray *filepart = [filePath componentsSeparatedByString:@"."];
    NSString *filename = [filepart objectAtIndex:0];
    NSString *extension = [filepart objectAtIndex:1];

    // Get the resource path and read the file using NSData
    NSString *file = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
    NSData *fileData = [NSData dataWithContentsOfFile:file];

    // Determine the MIME type
    NSString *mimeType;
    if ([extension isEqualToString:@"jpg"]) {
      mimeType = @"image/jpeg";
    } else if ([extension isEqualToString:@"png"]) {
      mimeType = @"image/png";
    } else if ([extension isEqualToString:@"doc"]) {
      mimeType = @"application/msword";
    } else if ([extension isEqualToString:@"docx"]) {
      mimeType = @"application/msword";
    } else if ([extension isEqualToString:@"ppt"]) {
      mimeType = @"application/vnd.ms-powerpoint";
    } else if ([extension isEqualToString:@"html"]) {
      mimeType = @"text/html";
    } else if ([extension isEqualToString:@"pdf"]) {
      mimeType = @"application/pdf";
    }

    // Add attachment
    [mailComposer addAttachmentData:fileData mimeType:mimeType fileName:filename];
    }

  [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:mailComposer animated:YES completion:nil];
    }

编辑:添加 filePath 的值,这就是我生成文件的方式

  NSAttributedString *str = [[NSAttributedString alloc] initWithAttributedString:_textV.attributedText];



  //convert to html then write to a .docx file to export to docx
  NSData *data = [str dataFromRange:(NSRange){0, [str length]} documentAttributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} error:NULL];

  [data writeToFile:@"/Users/myself/components/file.docx" atomically:YES];

【问题讨论】:

    标签: ios objective-c nsdata mfmailcomposeviewcontroller mfmailcomposer


    【解决方案1】:

    您提到的问题是这两行 - 如果文件是动态生成的,它不会在主包中 - 那是应用程序的包(OS X 和 iOS 上的每个应用程序都是一个文件夹/“bundle”或“package”/,其中包含其他文件。使用此方法,您正在检索与应用程序打包的文件。

    如果您正在生成内容,您可能会将其写入不同的路径(临时目录?) - 因此pathForResource: 方法返回nil(在应用程序包中找不到资源)和@987654323当路径为 nil 时,@ 初始化程序失败 -> 也返回 nil

    你很可能想要这个:NSData *fileData = [NSData dataWithContentsOfFile:filePath];

    几点说明:

    • 不要使用isEqual:nil。只需使用filePath != nil(更容易阅读,执行速度更快)。

    • NSString 有一个pathExtension 属性,它将为您检索该属性。

    • 非正式地反对使用文件路径,转而支持使用 URL:NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 您应该在新代码中使用 URL。

    如果这没有帮助,请发布filePath 的示例值。

    【讨论】:

    • 谢谢你,这更有意义。我没有考虑捆绑文件,也感谢您的提示。但是,我不明白如何使用动态生成的文件来做到这一点。
    • 您已经获得了一些文件路径作为 filePath 参数 - 您应该使用它作为路径,而不是从 NSBundle 重新检索它。如果没有有关您如何生成/保存文件的其他信息,我将无法提供更多帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多