如何在 iOS6 上将 Facebook 框架集成到应用程序中?

Posted

技术标签:

【中文标题】如何在 iOS6 上将 Facebook 框架集成到应用程序中?【英文标题】:How to integrate Facebook framework in app on iOS6? 【发布时间】:2012-09-30 02:58:37 【问题描述】:

我仍在使用旧的 facebook graph api 在我的应用程序的用户墙上发布内容。自 ios6 以来,facebook 已集成到操作系统中。我怎样才能用它来发布东西?我想像 twitter 框架一样使用它:

    TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
    [tweetViewController setInitialText:@"bla bla bla"];
    [self dismissModalViewControllerAnimated:YES];
    [self presentModalViewController:tweetViewController animated:YES];

【问题讨论】:

【参考方案1】:

从 iOS 6 开始,您可以将 SLComposeViewController 用于所有支持的服务类型,例如 Twitter、Facebook 和微博。 注意:不要忘记添加 Social.framework

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) 
     SLComposeViewController *fb = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
     [fb setInitialText:@"bla bla bla"];
     [self presentModalViewController:fb animated:YES];

【讨论】:

【参考方案2】:

如果您希望您的帖子“通过我的应用名称”发送,您可以使用 SLRequest:

-(IBAction)postMessage:(id)sender

    // Create the URL to the end point
    NSURL *postURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

    NSString *link = [[NSString alloc] init];
    NSString *message = [[NSString alloc] init];
    NSString *picture = [[NSString alloc] init];
    NSString *name = [[NSString alloc] init];
    NSString *caption = [[NSString alloc] init];
    NSString *description = [[NSString alloc] init];

    link = @"http://developer.apple.com/library/ios/#documentation/Social/Reference/Social_Framework/_index.html%23//apple_ref/doc/uid/TP40012233";
    message = @"Testing Social Framework";
    picture = @"http://www.stuarticus.com/wp-content/uploads/2012/08/SDKsmall.png";
    name = @"Social Framework";
    caption = @"Reference Documentation";
    description = @"The Social framework lets you integrate your app with supported social networking services. On iOS and OS X, this framework provides a template for creating HTTP requests. On iOS only, the Social framework provides a generalized interface for posting requests on behalf of the user.";

    NSDictionary *postDict = @
    @"link": link,
    @"message" : message,
    @"picture" : picture,
    @"name" : name,
    @"caption" : caption,
    @"description" : description
    ;

    SLRequest *postToMyWall = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:postURL parameters:postDict];

    FacebookAccountManager* sharedManager = [FacebookAccountManager sharedAccount];
    [postToMyWall setAccount:sharedManager.facebookAccount];

    [postToMyWall performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
    
        if (error) 
            // If there is an error we populate the error string with error
            _errorString = [NSString stringWithFormat:@"%@", [error localizedDescription]];

            // We then perform the UI update on the main thread. All UI updates must be completed on the main thread.
            [self performSelectorOnMainThread:@selector(updateErrorString) withObject:nil waitUntilDone:NO];
        

        else
        
            NSLog(@"Post successful");
            NSString *dataString = [[NSString alloc] initWithData:responseData encoding:NSStringEncodingConversionAllowLossy];
            NSLog(@"Response Data: %@", dataString);
        
     ];

    // Tidy Up
    link = nil;
    message = nil;
    picture = nil;
    name = nil;
    caption = nil;
    description = nil;
    postDict = nil;
    postToMyWall = nil;
    postURL = nil;


http://theworkingbear.com/social-framework-reference/ 了解完整演练

【讨论】:

以上是关于如何在 iOS6 上将 Facebook 框架集成到应用程序中?的主要内容,如果未能解决你的问题,请参考以下文章

ios6 facebook集成登录始终 FBSessionStateClosedLoginFailed 从不打开

如何在 iPhone 上从 Facebook 注销(本机/集成)

使用社交框架从 Facebook 注销

如何使用 SLRequest 在 iOS 6 中集成 Facebook?

iOS6 Facebook:如果用户没有配置Facebook怎么办?

装有 iOS 5 和 iOS 6 的 Facebook