5

正如我们所知道的,随着 ios 8 的推出,苹果允许自定义键盘扩展。在键盘扩展中,我们可以使用将图像复制到剪贴板。代码以 SMS 形式发送图像、gif 等

 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
 NSData *data= UIImagePNGRepresentation([UIImage imageNamed:@"so_close_disappointed_meme.png"]);
 [pasteboard setData:data forPasteboardType:@"public.png"];

现在我正在尝试像这个功能参考一样在 iMessage 中发送音频文件。不知道苹果会允许我们在 iMessage 中发送音频吗?所以我尝试了上述方法,但它没有在 SMS 窗口中显示任何音频粘贴选项。

 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
 NSString *path = [[NSBundle mainBundle] pathForResource:@"tune"ofType:@"mp3"];
 NSURL *url = [[NSURL alloc] initWithString:path];
 NSData *data = [NSData dataWithContentsOfURL:url];
 [pasteboard setData:data forPasteboardType:@"public.mp3"];

任何人都可以建议我如何使用自定义键盘扩展发送音频文件。这可能吗?

4

2 回答 2

9

I believe you could directly attach the file to MFMessageComposeViewController. Here is the documentation link of how it could be done.

Following would be the steps to do so.

  1. Check if file can be send using file UTI using + (BOOL)isSupportedAttachmentUTI:(NSString *)uti
  2. Find File UTI. i.e. path for the file
  3. Attach file to MFMessageComposeViewController using - (BOOL)addAttachmentData:(NSData *)attachmentData typeIdentifier:(NSString *)uti filename:(NSString *)filename

As the description for the method says

This method is especially useful when the attachment you want to add to a message does not have a file system representation. This can be the case, for example, for programmatically composed audiovisual content.

Note : You will have to convert audio file to NSData

于 2015-02-05T06:28:10.697 回答
0

MFMessageComposeViewController 不是这种情况下的解决方案。自定义键盘扩展不应呈现新的视图控制器,而只是将音频文件粘贴到粘贴板上。这是一些对我有用的快速代码

let path = NSBundle.mainBundle().pathForResource("audio", ofType:"wav")
let fileURL = NSURL(fileURLWithPath: path!)
let data = NSData(contentsOfURL: fileURL)
let wavUTI = "com.microsoft.waveform-audio"
UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: wavUTI)
于 2015-07-07T01:46:02.163 回答