iOS social sharing Twitter, Facebook, copy to clipboard, line, and mail

Source: Internet
Author: User

Get ready

The following three frameworks are introduced first:

Messageui.framework

Social.framework

Accounts.framework


and introduce the following several header files where these methods are implemented

#import <MessageUI/MFMailComposeViewController.h>

#import <Social/Social.h>

#import <Accounts/Accounts.h>


Twitter and Facebook

where urlstr for me to share the URL string, you can pass the content you want to share

Twitter, facebook-(void) Shareurl: (NSString *) urlstr viaslframework: (NSString *) sltype{//only support Fecebook and T Witter if ([Sltype Isequaltostring:slservicetypefacebook] | |    [Sltype Isequaltostring:slservicetypetwitter]) {if ([Slcomposeviewcontroller Isavailableforservicetype:slservicetypetwitter]) {Slcomposeviewco            Ntroller *socialcomposer = [Slcomposeviewcontroller composeviewcontrollerforservicetype:sltype];            [Socialcomposer Addurl:[nsurl urlwithstring:urlstr];            [Socialcomposer setcompletionhandler:^ (slcomposeviewcontrollerresult result)                {NSString *OUTSTR = [NSString new]; Switch (Result) {case slcomposeviewcontrollerresultcancelled:outstr = @ "Sharing failed!                        ";                    Break Case SLCOMPOSEVIEWCONTROLLERRESULTDONE:OUTSTR = @ "Sharing failed!                        ";                    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]; }    }}

Copy Content to Clipboard

URL copy-(void) Pasteurl: (NSString *) url{    //Copy text    uipasteboard *pasteboard = [Uipasteboard Generalpasteboard];    [Pasteboard Setstring:url];        Copy Picture/    *    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:@ " The content has been copied to the Clipboard "Delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil];    [Alert show];}


Line

Where urlstr is what I want to share. Share for the text

line-(void) Sharewithline: (NSString *) urlstr{//share text NSString *contenttype = @ "text";                           NSString *urlstring = [NSString stringwithformat:@ "line://msg/%@/%@",    ContentType, [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];    /****** share picture 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];    Get the picture from the Clipboard, the text can also be like this */nsurl *url = [Nsurl urlwithstring:urlstring];    Lorwylog (@ "%@", url);    if ([[[UIApplication sharedapplication] canopenurl:url]) {[[UIApplication sharedapplication] openurl:url]; } else{Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "Invalid URL" delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil];    [Alert show]; }}


MAIL

URLSTR Mail content, kmailaddress as the destination email address

Ps:self needs to implement the Mfmailcomposeviewcontrollerdelegate protocol to send a message after calling the second method below

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







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.