Search code examples
facebookios7uibuttonsharesocial-media

How to share app in social media through button click in iOS 7?


I am trying to Share my app through social media. Specifically fb, but I can't share my post.

I need to tap button specifically given in my design..

I used the coding below:

NSString *facebookShareLink = @"http://google.com";
NSString *titleToShare = @"Title";

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"fb://publish/profile/me?text=%@", facebookShareLink]];

if([[UIApplication sharedApplication] canOpenURL:url])
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookShareLink]  ];

} else
{

    NSString *facebookLink = [NSString stringWithFormat:@"https://www.facebook.com/login.php?next=https%3A%2F%2Fwww.facebook.com%2Fsharer%2Fsharer.php%3Fu%3D%2525%2540%26t%3D%2525%2540&display=popup", facebookShareLink, titleToShare];
    facebookLink = [facebookLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookLink]];
}

Which is taken from StackOverflow itself..

Can anyone help me to resolve this..


Solution

  • I try this.. Initially add sociamedia.framework to the project. Then import framework in class file .h or .m..

    -(IBAction)facebookPost:(id)sender{
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    SLComposeViewControllerCompletionHandler myBlock =  ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"Cancelled");
        }
        else
        {
            NSLog(@"Done");
        }
        [controller dismissViewControllerAnimated:YES completion:nil];
    };
    controller.completionHandler =myBlock;
    //Adding the Text to the facebook post value from iOS
    [controller setInitialText:@"My test post"];
    //Adding the URL to the facebook post value from iOS
    [controller addURL:[NSURL URLWithString:@"http://www.test.com"]];
    //Adding the Text to the facebook post value from iOS
    [self presentViewController:controller animated:YES completion:nil];
    }