web-dev-qa-db-ja.com

ACAccountStoreクラスを使用してObjective-CでiOS 6のFacebookに投稿する方法

Xcode 4.5の新しいフレームワークを使用して、iOS 6のFacebookにステータスメッセージを投稿する方法を知りたいです。ありがとう! :)

24
jaytrixz

メッセージの投稿はかなり簡単です。 Twitterフレームワークとほぼ同じです。

最初に、フレームワークをインポートする必要があります:ソーシャルおよびアカウント

#import <Social/Social.h>
#import <Accounts/Accounts.h>

.hファイルで:

SLComposeViewController *mySLComposerSheet;

このコードは、.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];
    }];
75
Blade

そして、投稿が成功した場合にアラートを受信したいだけで、ユーザーが投稿をキャンセルしても何もしなかった場合、何をしなければなりませんか?

残念ながら、これはTwitterで適切に機能しません...もうTweetSheetを却下しません。ここに私のコードがあります:

 if(NSClassFromString(@"SLComposeViewController") != nil)
 {

        mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
        mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social plattform to use it, e.g. facebook or Twitter
        [mySLComposerSheet setInitialText:[NSString stringWithFormat:story.title,mySLComposerSheet.serviceType]]; //the message you want to post
        [mySLComposerSheet addURL:[NSURL URLWithString:story.link]];
        //for more instance methodes, go here:https://developer.Apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//Apple_ref/doc/uid/TP40012205
        [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
            NSString *output;
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    output = NSLocalizedStringFromTable(@"As it seems you didn't want to post to Twitter", @"ATLocalizable", @"");
                    break;
                case SLComposeViewControllerResultDone:
                    output = NSLocalizedStringFromTable(@"You successfully posted to Twitter", @"ATLocalizable", @"");
                    break;
                default:
                    break;
            } //check if everything worked properly. Give out a message on the state.
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
        }];
        [self presentViewController:mySLComposerSheet animated:YES completion:nil];
1
Stephan König
- (IBAction)btn_facebook:(id)sender {

    SLComposeViewController *facebookcomposer =
        [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [facebookcomposer setInitialText:@"This is just a test"];
    [facebookcomposer addURL:[NSURL URLWithString:@"http://www.google.com"]];
    [facebookcomposer addImage:[UIImage imageNamed:@"images.jpg"]];

    [self presentViewController:facebookcomposer animated:YES completion:nil];

    [facebookcomposer setCompletionHandler:^(SLComposeViewControllerResult result)
    {
        switch (result)
        {
            case SLComposeViewControllerResultDone:
                NSLog(@"done");
                break;
            case SLComposeViewControllerResultCancelled:
                NSLog(@"cancelled");
                break;

            default:
            break;
        }

    }];

}

- (IBAction)btn_Twitter:(id)sender {

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *Twitter =
            [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [Twitter setInitialText:@"this is just a test"];
        [Twitter addURL:[NSURL URLWithString:@"http://www.google.com"]];
        [Twitter addImage:[UIImage imageNamed:@"images.jpg"]];
        [self presentViewController:Twitter animated:YES completion:nil];
    } else {
        NSLog(@"it is not configured");
    }
}
1
ashvin

しかし、これはアプリで実際に使用する方法です。よりスムーズで、コントローラーがハードワークを実行できるようにします。

- (IBAction)postToFacebook:(id)sender {
  if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])  {
        NSLog(@"log output of your choice here");
  }
  // Facebook may not be available but the SLComposeViewController will handle the error for us.
  self.mySLComposerSheet = [[SLComposeViewController alloc] init];
  self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
  [self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at SomeWhere:\n %@ \n", [self someURLString]]];
  [self.mySLComposerSheet addImage:self.photos.firstObject]; //an image you could post

  [self presentViewController:self.mySLComposerSheet animated:YES completion:nil];

  [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        NSString *output;
        switch (result) {
              case SLComposeViewControllerResultCancelled:
                    output = @"Action Cancelled";
                    break;
              case SLComposeViewControllerResultDone:
                    output = @"Post Successfull";
                    break;
              default:
                    break;
        }
        if (![output isEqualToString:@"Action Cancelled"]) {
              // Only alert if the post was a success. Or not! Up to you. 
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
              [alert show];
        }
  }];
}
0
FrostyL

わかりました、元の投稿を微調整し、iOS 6/7で動作します。 FacebookのServiceType、アラートタイトル、およびメッセージを変更します。楽しい!

- (IBAction)tweetMessage:(id)sender {


  if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) //check if Facebook Account is linked
  {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to Tweet!" message:@"Please login to Twitter in your device settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        return;
  }
  //self.mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
  self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
  [self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at this Place:\n %@ \n", [self someplace]]];
  [self.mySLComposerSheet addImage:self.photos.firstObject];

  [self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
  //}

  [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        NSString *output;
        switch (result) {
              case SLComposeViewControllerResultCancelled:
                    output = @"Action Cancelled";
                    break;
              case SLComposeViewControllerResultDone:
                    output = @"Post Successfull";
                    break;
              default:
                    break;
        } //check if everything worked properly. Give out a message on the state.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
  }];
}
0
FrostyL