我的应用程序正在用" .m4r"扩展名来播放URL播放铃声,我将它们放在一个名为ItemArray的数组中,例如:
- (void)viewDidLoad{
[super viewDidLoad];
self.title = @"Ringtones";
itemArray = [[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Song Title1", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Song Title2", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Song Title3", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Song Title4", @"song", @"Unknown", @"artise", @"http://www.mywebsite.com/", @"url", @"song.m4r", @"songId", nil], nil] retain];
}
现在我需要做的是在点击下载按钮时将这些文件附加到电子邮件的选项:
:- (void)downloadAudio:(DownloadButton *)dwnButton{
NSInteger index = dwnButton.tag;
NSString *selectedFile = [itemArray objectAtIndex:index];
[self showEmail:selectedFile];
}
- (void)showEmail:(NSString *)file{
if ([MFMailComposeViewController canSendMail]){
NSString *emailTitle = @"Ringtones";
NSString *messageBody = @"Enjoy our Awesome ringtones!";
NSArray *toRecipents = [NSArray arrayWithObject:@""];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
NSString *urlString = [NSString stringWithFormat:@"%@/%@", _audioPlayer.url, _audioPlayer.songId];
NSArray *filepart = [urlString componentsSeparatedByString:@"/"];
NSString *filename = [filepart lastObject];
// Get the resource path and read the file using NSData
NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:@"m4r"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// Add attachment
[mc addAttachmentData:fileData mimeType:@"audio/mpeg-4" fileName:filename];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:nil];
[mc release];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
switch (result){
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(@"Mail not sent.");
break;
}
// Remove the mail view
[self dismissViewControllerAnimated:YES completion:nil];
}
我的问题是每次点击下载按钮时,应用程序崩溃了"由于未知的例外NSInternalInconsistencyException
终止应用程序,原因:-[MFMailComposeInternalViewController addAttachmentData:mimeType:fileName:] attachment must not be nil.
***第一次投掷呼叫堆栈:libc abi.dylib:终止以未被发现的类型nsexception"错误"。
[btw:该应用在我的真实设备上不是模拟器上运行]
任何帮助将不胜感激。
[mfmailComposeInternalViewController addattachmentdata:mimeType:filename:]附件不得nil
调用[mc addAttachmentData:fileData mimeType:@"audio/mpeg-4" fileName:filename];
时,将以邮件中的附件发送的fileData
为nil
。那是坠机的根源。
不建议通过dataWithContentsOfFile:
获取远程内容。
您必须考虑使用NSURLSession
API。
这个&这是关于该主题的好读物。
替代实现可以是:
- (void)showEmail:(NSString *)file{
if([MFMailComposeViewController canSendMail]){
NSURLSession *sessionWithoutDelegate = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSString *urlString = [NSString stringWithFormat:@"%@/%@", _audioPlayer.url, _audioPlayer.songId];
NSURL *url = [NSURL URLWithString:urlString];
[[sessionWithoutDelegate dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *emailTitle = @"Ringtones";
NSString *messageBody = @"Enjoy our Awesome ringtones!";
NSArray *toRecipents = [NSArray arrayWithObject:@""];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
NSString *urlString = [NSString stringWithFormat:@"%@/%@", _audioPlayer.url, _audioPlayer.songId];
NSArray *filepart = [urlString componentsSeparatedByString:@"/"];
NSString *filename = filepart.lastObject;
// use the downloaded data here
[mc addAttachmentData:data mimeType:@"audio/mpeg-4" fileName:filename];
[self presentViewController:mc animated:YES completion:nil];
}] resume];
}
else{
//...
}
}