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];
    }
}


MAIL

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];
}







iOS社交分享Twitter、Facebook、复制到剪切板、LINE、及邮件,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。