【问题标题】:iOS open Facebook and Twitter app share pageiOS 打开 Facebook 和 Twitter 应用分享页面
【发布时间】:2014-10-07 19:17:28
【问题描述】:

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

Twitter 也是如此。

【问题讨论】:

    标签: ios facebook twitter share


    【解决方案1】:

    您可以使用图片和文字轻松发布到 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];
        }
    

    }

    对于高音扬声器,请执行以下操作:

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

    对于高音:

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

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

    【讨论】:

    • 不要忘记通过转到 BuildPhases -> LinkBinaryWithLibraries 添加 Social.framework。同样在 ViewController 中不要忘记在顶部导入 #import
    • @michael > 谢谢。已添加。
    • @Rashad 谢谢你的回复 这很有用但我必须打开应用程序并将其发布到那里。有什么办法吗?
    • @Rashad 谢谢Rshad。我也可以在推特上使用它吗?
    • 你当然可以在 tweeter 应用程序中打开页面,但我不确定 text 参数是否有效。
    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2014-11-13
    • 2012-07-04
    • 2016-12-02
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多