Ios-twitter-facebook

提供:Dev Guides
移動先:案内検索

iOS-TwitterおよびFacebook

Twitterは iOS 5.0 に統合され、Facebookは iOS 6.0 に統合されました。 このチュートリアルでは、Appleが提供するクラスの使用に焦点を当てており、TwitterとFacebookの展開ターゲットはそれぞれiOS 5.0とiOS 6.0です。

関与するステップ

  • ステップ1 *-シンプルなビューベースのアプリケーションを作成します。

ステップ2 *-プロジェクトファイルを選択してから、 *targets を選択し、 Social.framewor * kおよび Accounts.frameworkchoose frameworks に追加します。

  • ステップ3 *-facebookPostとtwitterPostという名前の2つのボタンを追加し、それらのibActionsを作成します。

ステップ4 *-次のように *ViewController.h を更新します-

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

@interface ViewController : UIViewController

-(IBAction)twitterPost:(id)sender;
-(IBAction)facebookPost:(id)sender;

@end

ステップ5 *-次のように *ViewController.m を更新します-

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
  //Dispose of any resources that can be recreated.
}

-(IBAction)facebookPost:(id)sender {
   SLComposeViewController *controller = [SLComposeViewController
   composeViewControllerForServiceType:SLServiceTypeFacebook];
   SLComposeViewControllerCompletionHandler myBlock =
      ^(SLComposeViewControllerResult result){

      if (result == SLComposeViewControllerResultCancelled) {
         NSLog(@"Cancelled");
      } else {
         NSLog(@"Done");
      }
      [controller dismissViewControllerAnimated:YES completion:nil];
   };
   controller.completionHandler = myBlock;

  //Adding the Text to the facebook post value from iOS
   [controller setInitialText:@"My test post"];

  //Adding the URL to the facebook post value from iOS
   [controller addURL:[NSURL URLWithString:@"http://www.test.com"]];

  //Adding the Text to the facebook post value from iOS
   [self presentViewController:controller animated:YES completion:nil];
}

-(IBAction)twitterPost:(id)sender {
   SLComposeViewController *tweetSheet = [SLComposeViewController
   composeViewControllerForServiceType:SLServiceTypeTwitter];
   [tweetSheet setInitialText:@"My test tweet"];
   [self presentModalViewController:tweetSheet animated:YES];
}
@end

出力

アプリケーションを実行してfacebookPostをクリックすると、次の出力が得られます-

iOSチュートリアル

twitterPostをクリックすると、次の出力が得られます-

iOSチュートリアル