Learn how to share on social media using Social Framework

The most common feature in every app is the social sharing which helps you grow your audience. However, integrating SDK’s from Facebook, Twitter, and other social networks can be a real pain and time consuming especially if you are going to use only the share option and not their other SDK features.

That’s why Xcode offers you a native framework called Social where Facebook and Twitter sharing is handled in a really easy and simple way.

 

Facebook share:

-(void)shareOnFacebook{
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        controller.completionHandler = ^(SLComposeViewControllerResult result) {
            
            switch(result)
            {
                    //  This means the user cancelled without sending the Facebook post
                case SLComposeViewControllerResultCancelled:
                    break;
                    //  This means the user hit 'Send'
                case SLComposeViewControllerResultDone:
                    break;
            }
        };
        
        [controller addURL:[NSURL URLWithString:@"www.theappspace.com"]];
        [self presentViewController:controller animated:YES completion:nil];
                    
    }
    else
    {
    // No facebook account, you can handle error here! 
    }
}

 

Twitter share:

-(void)shareOnTwitter{    
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
            switch(result)
            {
                    //  This means the user cancelled without sending the Tweet
                case SLComposeViewControllerResultCancelled:
                    break;
                    //  This means the user hit 'Send'
                case SLComposeViewControllerResultDone:
                    break;
            }
        };
        
        [tweetSheet addURL:[NSURL URLWithString:@"www.theappspace.com"]];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else
    {
    No Twitter account set, handle error here!
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *