您的位置:首页 > 移动开发

iOS 通过邮件分享app生成的视频

2016-09-23 09:05 513 查看
最近在研究app内的视频 分享出去,最好能分享到其他第三方应用如twitter,Facebook,微信,微博等,网上全是一些什么SDK,都是只能分享图片,url等,并不是我想要的,最后实在没办法了,只能用分享邮件的方式,把视频分享出去。好了下面开始分享代码!

.h里面

#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
- (IBAction)SendMail:(id)sender;
@end


.m文件里面实现为

- (IBAction)SendMail:(id)sender {

MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
if(mailCompose)
{
//设置代理
[mailCompose setMailComposeDelegate:self];

NSArray *toAddress = [NSArray arrayWithObject:@"Pleaseset@ToRecipients.com"];
NSArray *ccAddress = [NSArray arrayWithObject:@"Pleaseset@ccAddress.com"];
NSString *emailBody = @"The body of mail!";

//设置收件人
[mailCompose setToRecipients:toAddress];
//设置抄送人
[mailCompose setCcRecipients:ccAddress];
//设置邮件内容
[mailCompose setMessageBody:emailBody isHTML:YES];
//设置邮件主题
[mailCompose setSubject:@"The body of mail!"];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"testAVI" ofType:@".avi"];
NSArray *strArr = [videoPath componentsSeparatedByString:@"/"];
int temp=0;
for (NSArray *secKey in strArr){
temp++;
}
NSString *Name = [NSString stringWithFormat:@"%@", [strArr objectAtIndex:temp-1]];
NSData *videoData = [NSData dataWithContentsOfFile: videoPath];
[mailCompose addAttachmentData:videoData mimeType:@"application/avi" fileName:Name];
//设置邮件视图在当前视图上显示方式
[self presentModalViewController:mailCompose animated:YES];
}
return;
}


这样就可以发送视频到邮件了,但是还需要一个代理,发送完退出present的邮件窗口;

- (void)alertWithTitle:(NSString *)title  msg:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{

NSString *msg;

switch (result)
{
case MFMailComposeResultCancelled:
msg = NSLocalizedString(@"Sent cancel", @"");
break;
case MFMailComposeResultSaved:
msg = NSLocalizedString(@"Save Successfull!", @"");
[self alertWithTitle:nil msg:msg];
break;
case MFMailComposeResultSent:
msg = NSLocalizedString(@"Sent Successfull!", @"");
[self alertWithTitle:nil msg:msg];
break;
case MFMailComposeResultFailed:
msg = NSLocalizedString(@"Sent Fail!", @"");
[self alertWithTitle:nil msg:msg];
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}


邮件分享是无法在模拟器上运行的,必须要真机,上面的代码,地址取的是外面拖进project的名字自动取得的,但是在app里面,我们如何取得app自己生成的地址呢?

这就需要 enumeratorAtPath 来遍历app中的视频

详细可见这篇博客
http://blog.csdn.net/p2game/article/details/17766427
由于本人技术有限,如有写的不科学的地方,还请指出来,大家一起交流!

如果大家有分享视频的好方法,也希望与博主分享一下,共同进步!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: