码迷,mamicode.com
首页 > 移动开发 > 详细

iOS社交分享Twitter、Facebook、复制到剪切板、LINE、及邮件

时间:2014-07-18 18:05:22      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:ios   社交   objective-c   facebook   邮件   

准备

首先要引进如下三个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、及邮件,布布扣,bubuko.com

iOS社交分享Twitter、Facebook、复制到剪切板、LINE、及邮件

标签:ios   社交   objective-c   facebook   邮件   

原文地址:http://blog.csdn.net/lorwy/article/details/37928069

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!