准備
首先要引進例如以下三個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]; }