iOS打开fb和twitter app共享页面

时间:2014-08-14 08:03:15

标签: ios facebook twitter share

我想从我的应用程序中打开facebook和twitter应用程序。同时我有一个文本,必须在Facebook共享框中自动填充。我该如何实现这一目标?我现在可以打开应用程序了。但是,如何打开共享页面并传递要填充的文本。请帮忙。

与推特相同

1 个答案:

答案 0 :(得分:0)

您可以使用图片和文字轻松发布到Facebook。为此,您不必退出您的应用程序。试试这个:

- (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];
    }

}

对于tweeter,请执行以下操作:

- (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];
    }
}

不要忘记通过转到

添加SOCIAL.FRAMEWORK
  

BuildPhases - > LinkBinaryWithLibraries

同样在ViewController中,不要忘记在顶部导入#import <Social/Social.h>

修改

我在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]];
}

对于tweeter:

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

希望这会有所帮助.. :)