简体   繁体   中英

iOS open fb and twitter app share page

I want to open the facebook and twitter app from my application. At the same time I have a text which has to be auto populated in the facebook share box. How do I achieve this? I am able to open the app now. But how do I open the share page and pass the text to be populated. Please help.

The same with twitter

You can easily post to facebook with image and text. For this you don't have to exit your app. Try this:

- (void) postFacebook {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [fbSheet setInitialText:@"Your Text here"];
        [fbSheet addImage:[UIImage imageNamed:@"Icon-144.png"]];
        [fbSheet addURL:[NSURL URLWithString:@"http://asdf.com"]];
        [[CCDirector sharedDirector] presentViewController:fbSheet animated:YES completion:nil];
    }
    else {

        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry!!!"
                                  message:@"You can't share on facebook right now, make sure your device has an internet connection and you have at least one facebook account setup in your device."
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }

}

For tweeter do this:

- (void) callTwitter {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Your text here"];
        [tweetSheet addImage:[UIImage imageNamed:@"Icon-144.png"]];
        [tweetSheet addURL:[NSURL URLWithString:@"http://asdf.com"]];
        [[CCDirector sharedDirector] presentViewController:tweetSheet animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry!!!"
                                  message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one twitter account setup in your device."
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

Don't forget to add the SOCIAL.FRAMEWORK by going to

BuildPhases -> LinkBinaryWithLibraries

Also in the ViewController don't forget to import at the top #import <Social/Social.h>

EDIT:

Well I found this in SO:

NSString *myMessage = [NSString stringWithFormat:@"%@",
                       [self.txtAdreca.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *post = [NSString stringWithFormat:@"fb://publish/profile/me?text=%@",urlString];
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:post]];
if (canOpenURL) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:post]];
}

For tweeter:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://status?id=12345"]];

Hope this helps.. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM