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

Source: Internet
Author: User

Get ready

The first is to introduce three frameworks such as the following:

Messageui.framework

Social.framework

Accounts.framework


and introduce the following header files where these methods are implemented

#import <MessageUI/MFMailComposeViewController.h>

#import <Social/Social.h>

#import <Accounts/Accounts.h>


Twitter and Facebook

Among the URL strings that urlstr share for me, 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:@ " Content has been copied to the Clipboard "Delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil];    [Alert show];}


Line

Among them, 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 so */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 need to implement the Mfmailcomposeviewcontrollerdelegate protocol to send a message after calling the following second method

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 social sharing Twitter, Facebook, copy to clipboard, line, and mail

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.