2012-09-20 1 views
24

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

+0

チュートリアルの素敵なセットがここにあります:http://developers.facebook.com/docs/getting-started/getting-started-with-the-ios-sdk/ –

+0

いいえ、私は意味iOS 6の新しいフレームワークを使用してください。 – jaytrixz

+1

詳細については、ソーシャルフレームワークを使用してください。http://kmithi.blogspot.in/2012/10/integrating-facebook-and-twitter-in-ios.html – mithilesh

答えて

75

メッセージを投稿するのは簡単です。それはTwitterフレームワークとほとんど同じです。

まずあなたがフレームワークをインポートする必要があります。社会と自分の.hファイルで

#import <Social/Social.h> 
#import <Accounts/Accounts.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]; 
    }]; 
+0

@Blade thans。しかし、私は "TWRequest"のようなアクションシートなしでメッセージを共有したいので、Facebookのプロセスは何ですか? – Hitarth

+0

@Blade Grt suggetion。その作業がいいですが、私は2つのポップアップを取得しているのと同じメッセージを返すときに1つのクエリを持っています。 1.投稿は成功しました。2. Facebookへの接続が失敗したため投稿を送信できません。私はただ1つのポップアップがほしいのでユーザーはアイデアを得ます。この2人のポップアップユーザーが成功したかどうかを混乱させてしまいます。あなたが何か考えているなら私に示唆してください。 – Hitarth

+1

@Bladeは「iOS経由でリンクを共有する」を「MyAppName経由でリンクを共有する」に変更できますか? – Hitarth

1

投稿が成功した場合にアラートを受信したい場合はどうすればよいですか。ユーザーが投稿をキャンセルしたときは何も表示されません。

残念ながらこれは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]; 
0

[OK]をみんな、私はオリジナルのポストを微調整し、iOSの6/7で動作します。 Facebookのサービスタイプ、アラートタイトル、メッセージの変更楽しい!

- (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

これは私のアプリで実際に使用する方法です。より滑らかになり、コントローラがハードワークを行うことができます。

- (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]; 
     } 
    }]; 
} 
1
- (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"); 
    } 
} 
関連する問題