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








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM