Tuesday, 26 July 2016

Twitter and Facebook sharing option in iOS

Today we will learn how to give share option (Facebook and twitter) in iOS using Social framework in iOS.

Steps:
1. Create a Xcode project Lets say (SocialFrameworkDemo).
2. Add Social Framework library in Your Project.
 3. Now go to StoryBoard and create two buttons and make Proper connection.

4.  You can add images in Your ProjectFolder if You want to share images also.
5. Now go to Your ViewController.m and add following code.
#import "ViewController.h"
#import <Social/Social.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)fbButtonAction:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
         SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [controller setInitialText:@"iostpoint - Everything about iOS"];
        [controller addImage:[UIImage imageNamed:@"facebookLogo"]];
        [controller addURL:[NSURL URLWithString:@"https://www.iostpoint.wordpress.com"]];
        [self presentViewController:controller animated:YES completion:nil];
    }
    else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Facebook not installed or confogured" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}

- (IBAction)twitterButtonAction:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
         [controller setInitialText:@"iostpoint - Everything about iOS"];
        [controller addImage:[UIImage imageNamed:@"twitterLogo"]];
          [controller addURL:[NSURL URLWithString:@"https://www.iostpoint.blogspot.com"]];
        [self presentViewController:controller animated:YES completion:nil];
    }
    else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Facebook not installed or confogured" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

6. Run the code and see the desired output. You can download the whole source code from here.
Please give your valuable feedback.
Enjoy Coding :) 

2 comments:

Ads Inside Post

Blogger Widget