iOS社交分享Twitter、Facebook、拷贝到剪切板、LINE、及邮件
准备
首先要引进例如以下三个framework:
MessageUI.framework
Social.framework
Accounts.framework
并在实现这几个方法的地方引入下面几个头文件
#import <MessageUI/MFMailComposeViewController.h>
#import <Social/Social.h>
#import <Accounts/Accounts.h>
Twitter及Facebook
当中urlStr为我分享的url字符串,你能够传你想分享的内容
//Twitter 、Facebook - (void)shareUrl:(NSString *)urlStr ViaSLFrameWork:(NSString *)slType { //only support fecebook and twitter if ([slType isEqualToString:SLServiceTypeFacebook] || [slType isEqualToString:SLServiceTypeTwitter]) { if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) { SLComposeViewController *socialComposer = [SLComposeViewController composeViewControllerForServiceType:slType]; [socialComposer addURL:[NSURL URLWithString:urlStr]]; [socialComposer setCompletionHandler:^(SLComposeViewControllerResult result) { NSString *outStr = [NSString new]; switch (result) { case SLComposeViewControllerResultCancelled: outStr = @"分享失败。"; break; case SLComposeViewControllerResultDone: outStr = @"分享失败!"; break; default: break; } UIAlertView *myalertView = [[UIAlertView alloc]initWithTitle:nil message:outStr delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [myalertView show]; }]; [self presentViewController:socialComposer animated:YES completion:nil]; } } }
复制内容到剪切板
//URL复制 - (void)pasteUrl:(NSString *)url { //复制文字 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:url]; //复制图片 /* UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setData:UIImageJPEGRepresentation([UIImage imageNamed:@"account_icon_friend.png"] , 1.0) forPasteboardType:@"public.jpeg"];*/ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"内容已拷贝到剪切板" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; }
LINE
当中urlStr为我要分享的内容。分享的为Text
//LINE - (void)shareWithLine:(NSString *)urlStr { //分享文字 NSString *contentType = @"text"; NSString *urlString = [NSString stringWithFormat:@"line://msg/%@/%@", contentType, [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; /******分享图片 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setData:UIImageJPEGRepresentation([UIImage imageNamed:@"account_icon_friend.png"] , 1.0) forPasteboardType:@"public.jpeg"]; NSString *contentType = @"image"; NSString *urlString = [NSString stringWithFormat:@"line://msg/%@/%@", contentType, pasteboard.name]; //从剪切板中获取图片,文字亦能够如此 */ NSURL *url = [NSURL URLWithString:urlString]; LorwyLog(@"%@",url); if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无效的url" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } }
urlStr邮件内容,kMailAddress为目的邮件地址
PS:self须要实现MFMailComposeViewControllerDelegate协议才会发送邮件后调用以下第二个方法
//MAIL - (void)shareUrlMail:(NSString *)urlStr { if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; [mailViewController setSubject:kMailAddress]; [mailViewController setMessageBody:urlStr isHTML:NO]; mailViewController.mailComposeDelegate = self; mailViewController.navigationBar.tintColor = [UIColor blackColor]; [self presentViewController:mailViewController animated:YES completion:nil]; } } - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; }