ios 使用 mfMailComposer 发送 docx 附件失败

标签 ios objective-c nsdata mfmailcomposeviewcontroller mfmailcomposer

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

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

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

= [NSData dataWithContentsOfFile:文件];

这是代码

    -(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];

最佳答案

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

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

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

一些注意事项:

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

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

  • 使用文件路径已被非正式地弃用,取而代之的是使用 URL:NSURL *fileURL = [NSURL fileURLWithPath:filePath];您应该在新代码中使用 URL。

    <

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

关于ios 使用 mfMailComposer 发送 docx 附件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34525710/

相关文章:

iphone - 我可以在iPhone Simulator的配置文件中使用数据吗?

ios - FaSTLane 扫描显示零单元测试

ios - addTarget 使用安全吗?

iphone - 使用 CCSpriteFrameCache 为动画 Sprite 添加标签

ios - 数据未从 iphone 传输到真实设备上的 iWatch (AppleWatch)

objective-c - 从偏好中读取值

objective-c - iOS 在 View 之间传递消息,基于窗口的应用程序

swift - 结构到 NSData

swift - 在 Swift 中的 AsynchronousRequest 之后,NSData 为 nil

ios - 将 NSData 转换为 CMSampleBufferRef