iOS社交分享Twitter、Facebook、拷貝到剪切板、LINE、及郵件

準備

首先要引進例如如下三個framework:post

MessageUI.framework
url

Social.framework
spa

Accounts.framework
code


並在實現這幾個方法的地方引入下面幾個頭文件orm

#import <MessageUI/MFMailComposeViewController.h>圖片

#import <Social/Social.h>ci

#import <Accounts/Accounts.h>字符串


Twitter及Facebook

當中urlStr爲我分享的url字符串,你可以傳你想分享的內容string

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


複製內容到剪切板

//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];
}
相關文章
相關標籤/搜索