做分享功能时,通常集成SDK,进行微博、微信、qq分享。不知道你们用过系统自带的分享功能没,下面说一下系统自带的分享功能。很简单的。
1.首先要导入头文件 #import <Social/Social.h>
2.创建一个分享button
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor magentaColor];
[button setTitle:@"分享" forState:UIControlStateNormal];
[button addTarget:self action:@selector(touchAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
3.button的点击事件就是触发分享
-(void)touchAction{
//初始化
SLComposeViewController *compose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];//微博分享,里面类型还有微信、FaceBook
//判断
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
NSLog(@"没有安装相对应得软件");
return;
}
[compose setInitialText:@"找工作"];
//可以写你想要分享的图片
[compose addImage:[UIImage imageNamed:@" "]];
[compose addURL:[NSURL URLWithString:@"http://baidu.com"]];
__block SLComposeViewController *comVc = compose;//回调
compose.completionHandler = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultDone) {
NSLog(@"分享成功");
}
[comVc dismissViewControllerAnimated:YES completion:^{
//成功之后可以写其他方法
}];//分享成功跳出
};
[self presentViewController:compose animated:YES completion:nil];
}