Alexis
Alexis

Reputation: 16829

How to share something on facebook from my iOS app?

I'm adding share functionalities to my iOS app (I have to support down to iOS 4.3). In the Facebook case, the behavior I want to implement is:

if the facebook app is present on the iDevice then open it and go to share view with everything already pre-filled

else open www.facebook.com/sharer.php?[etc] in safari (or whatever browser) to share my page.

I wrote this :

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

    if([[UIApplication sharedApplication] canOpenURL:url]) {

        [[UIApplication sharedApplication] openURL:url];

    } else {

        NSString *facebookLink = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?u=%@&t=%@", facebookShareLink, titleToShare];
        facebookLink = [facebookLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookLink]];
    }

When the Facebook app is NOT installed it works pretty well but when it's installed then it opens but it won't go to the share view.

I read somewhere that the URL schemes are not supported anymore (I don't have the source). Is that true?

Then is there still a way to do this without integrating the Facebook iOS SDK? I don't want to add this SDK, I just need the sharing functionality not all the Graph stuff.

EDIT: I don't want my app to share itself sth on a social media but to prompt the user and give him the ability to do it himself easily. I think I shouldn't have to authenticate with a Facebook app or a Twitter app in order to do that, should I? The user will authenticate himself in the app or on the mobile website.

Upvotes: 2

Views: 6874

Answers (4)

Florian Marcu
Florian Marcu

Reputation: 1

For those of you looking for a Swift 3 solution, here it is:

if let composeVC = SLComposeViewController(forServiceType:serviceType) {
    composeVC.add(object.image())
    composeVC.add(object.url())
    composeVC.setInitialText(object.text())
    presentingVC.present(composeVC, animated: true, completion: nil)
}

Upvotes: 1

Sabrina Tuli
Sabrina Tuli

Reputation: 160

you can share in facebook, twitter or by email using uiactivity

UIImage *image1 = shareimgview.image;
NSString *str1=[@"Brand: " stringByAppendingString:brand_field.text];
NSString *str2=[@"Product: " stringByAppendingString:product_field.text];
NSString *str3=[@"Model: "stringByAppendingString:model_field.text];
NSString *str4=[@"Comment: "stringByAppendingString:status_field.text];
NSArray* dataToShare = @[str1,myurl,str2,str3,str4, image1];
UIActivityViewController *actviewcon = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[self presentViewController:actviewcon animated:YES completion:NULL];

Upvotes: 1

Alexis
Alexis

Reputation: 16829

I finally made a solution using the iOS built-in facebook share dialog (SLComposeViewController) that falls back on the website if the user is not using iOS 6

NSString *facebookShareLink = @"some link to share";
NSString *titleToShare = @"some title";

// if we're not on iOS 6, SLComposeViewController won't be available. Then fallback on website.
Class composeViewControllerClass = [SLComposeViewController class];

if(composeViewControllerClass == nil || ![composeViewControllerClass isAvailableForServiceType:SLServiceTypeFacebook]) {

    NSString *facebookLink = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?u=%@&t=%@", facebookShareLink, titleToShare];
    facebookLink = [facebookLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:facebookLink]];

} else {

    SLComposeViewController *composeViewController = [composeViewControllerClass composeViewControllerForServiceType:SLServiceTypeFacebook];
    [composeViewController setInitialText:titleToShare];
    [composeViewController addURL:[NSURL URLWithString:facebookShareLink]];

    [parentController presentModalViewController:composeViewController animated:YES];
}

Upvotes: 3

Basem Saadawy
Basem Saadawy

Reputation: 1818

you can use third party components such as ShareKit.
http://getsharekit.com/

Upvotes: 1

Related Questions