IOS-Twitter和(hé)Facebook
簡介
Twitter已經整合到(dào)iOS5.0,而Facebook已經被集成在 iOS 6.0中。本教程的重點講解如何利用(yòng)蘋果提供的類在iOS5.0和(hé)iOS6.0中部署Twitter和(hé)Facebook。
實例步驟
1. 創建一個簡單View based application
2. 選擇項目文(wén)件,然後選擇"targets(目标)",然後在 choose frameworks(選擇框架)中添加Social.framework 和(hé) Accounts.framework
3. 添加兩個名爲facebookPost 和(hé) twitterPost的按鈕,并爲他(tā)們創建 ibActions。
4. 更新 ViewController.h 如下(xià)
#import
#import
#import
@interface ViewController : UIViewController
-(IBAction)twitterPost:(id)sender;
-(IBAction)facebookPost:(id)sender;
@end
5. 更新ViewController.m ,如下(xià)所示
#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
網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發