iOS Twitter和Facebook

2018-11-21 17:00 更新

IOS-Twitter和Facebook


簡(jiǎn)介

Twitter已經(jīng)整合到iOS5.0,而Facebook已經(jīng)被集成在 iOS 6.0中。本教程的重點(diǎn)講解如何利用蘋果提供的類在iOS5.0和iOS6.0中部署Twitter和Facebook。

實(shí)例步驟

1. 創(chuàng)建一個(gè)簡(jiǎn)單View based application

2. 選擇項(xiàng)目文件,然后選擇"targets(目標(biāo))",然后在 choose frameworks(選擇框架)中添加Social.framework 和 Accounts.framework

3. 添加兩個(gè)名為facebookPost 和 twitterPost的按鈕,并為他們創(chuàng)建 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

輸出

運(yùn)行該應(yīng)用程序并單擊 facebookPost 時(shí)我們將獲得以下輸出

iOS Twitter和Facebook

當(dāng)我們單擊 twitterPost 時(shí),我們將獲得以下輸出

iOS Twitter和Facebook

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)