如何使用ACAccountStore类在Objective-C中在iOS 6上发布到Facebook

jay*_*ixz 24 xcode frameworks facebook posting ios6

我想知道如何使用Xcode 4.5上的新框架在iOS 6上向Facebook发布状态消息.谢谢!:)

Bla*_*ade 75

发布消息非常简单.它几乎与Twitter框架一样.

首先,您必须导入框架:社交和帐户

#import <Social/Social.h>
#import <Accounts/Accounts.h>
Run Code Online (Sandbox Code Playgroud)

在你的.h文件中:

SLComposeViewController *mySLComposerSheet;
Run Code Online (Sandbox Code Playgroud)

此代码必须包含在.m文件中的操作中:

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
    {
      mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
        mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social platform to use it, e.g. facebook or twitter
                [mySLComposerSheet setInitialText:[NSString stringWithFormat:@"Test",mySLComposerSheet.serviceType]]; //the message you want to post
       [mySLComposerSheet addImage:yourimage]; //an image you could post
        //for more instance methods, go here: https://developer.apple.com/documentation/social/slcomposeviewcontroller#//apple_ref/doc/uid/TP40012205
        [self presentViewController:mySLComposerSheet animated:YES completion:nil];
    }
    [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        NSString *output;
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                output = @"Action Cancelled";
                break;
            case SLComposeViewControllerResultDone:
                output = @"Post Successful";
                break;
            default:
                break;
        } //check if everything worked properly. Give out a message on the state.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];
Run Code Online (Sandbox Code Playgroud)