📅  最后修改于: 2020-12-08 06:26:13             🧑  作者: Mango
Twitter已集成到iOS 5.0中,而Facebook已集成到iOS 6.0中。我们的教程侧重于使用Apple提供的类,Twitter和Facebook的部署目标分别是iOS 5.0和iOS 6.0。
步骤1-创建一个简单的基于视图的应用程序。
步骤2-选择您的项目文件,然后选择目标,然后在选择框架中添加Social.framewor k和Accounts.framework 。
步骤3-添加两个名为facebookPost和twitterPost的按钮,并为它们创建ibActions。
步骤4-更新ViewController.h如下-
#import
#import
#import
@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时,我们将获得以下输出-
单击twitterPost时,将获得以下输出-