1.Facebook官方的SDK分享
2.ShareSDK,第三方集成的分享方式
3.网页分享方式分享
4.IOS6之后,苹果自己集成了对于Facebook和Twitter等社交分享进行了集成
1.加入Social.framework
2.#import <Social/Social.h>
3.声明变量SLComposeViewController *mySLComposerSheet;//share对象
注:1.如果你的当前的app只需要满足IOS6及以上系统的用户的需求(毕竟IOS5及以下的用户量很少),那么就不需要考虑太多,直接使用系统的分享方式。如下:(建议做个异常判断,毕竟崩溃也不好)
2.如果手机上自带的应用已经绑定账号,那么可以直接分享。
3.如果手机上安装了这些应用的客户端,自带的应用绑定了账号,那么他会优先客户端分享,没有登录会提示你登录。
4.如果客户端和自带的都没绑定账号,SLComposeViewController 的对象为nil 会崩溃,可以提前捕捉到,加个判断就行。
- (void)shareFacebook{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=6){
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self.mySLComposerSheet setInitialText:self.productDetailModel.productName];//产品名
NSURL *imageUrl = [NSURL URLWithString:self.productDetailModel.primaryThumbimage];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
[self.mySLComposerSheet addImage:[UIImage imageWithData:imageData scale:1]];//产品图
[self.mySLComposerSheet addURL:[NSURL URLWithString:self.productDetailModel.productUrl]];//产品地址
[self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}else{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.view myMBProgressShowView:@"Not Available Facebook!"];
}
[self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = MBLocalizedString(kFacebookActionCancell);
break;
case SLComposeViewControllerResultDone:
output = MBLocalizedString(kFacebookPostSuccess);
break;
default:
break;
}
if ((result = SLComposeViewControllerResultCancelled)) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:MBLocalizedString(kFacebookMessage) message:output delegate:nil cancelButtonTitle:MBLocalizedString(kFacebookOK) otherButtonTitles:nil];
[alert show];
}
}];
}
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=6){
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self.mySLComposerSheet setInitialText:self.productDetailModel.productName];//产品名
NSURL *imageUrl = [NSURL URLWithString:self.productDetailModel.primaryThumbimage];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
[self.mySLComposerSheet addImage:[UIImage imageWithData:imageData scale:1]];//产品图
[self.mySLComposerSheet addURL:[NSURL URLWithString:self.productDetailModel.productUrl]];//产品地址
[self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}else{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.view myMBProgressShowView:@"Not Available Facebook!"];
}
[self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = MBLocalizedString(kFacebookActionCancell);
break;
case SLComposeViewControllerResultDone:
output = MBLocalizedString(kFacebookPostSuccess);
break;
default:
break;
}
if ((result = SLComposeViewControllerResultCancelled)) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:MBLocalizedString(kFacebookMessage) message:output delegate:nil cancelButtonTitle:MBLocalizedString(kFacebookOK) otherButtonTitles:nil];
[alert show];
}
}];
}
}
同样使用Social框架可以分享到Twitter,新浪微博,腾讯微博,前提是你手机上绑定了这些帐号,否则怎么知道使用哪个帐号分享,在iOS中有专门设置这些应用的地方
//下面是新浪的
-(void)shareToSina{
//检查新浪微博服务是否可用
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]){
NSLog(@"新浪微博服务不可用.");
return;
}
//初始化内容编写控制器,注意这里指定分享类型为新浪微博
SLComposeViewController *composeController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
//设置默认信息
[composeController setInitialText:@"Kenshin Cui‘s Blog..."];
//添加图片
[composeController addImage:[UIImage imageNamed:@"stevenChow"]];
//添加连接
[composeController addURL:[NSURL URLWithString:@"http://www.cnblogs.com/kenshincui"]];
//设置发送完成后的回调事件
__block SLComposeViewController *composeControllerForBlock=composeController;
composeController.completionHandler=^(SLComposeViewControllerResult result){
if (result==SLComposeViewControllerResultDone) {
NSLog(@"开始发送...");
}
[composeControllerForBlock dismissViewControllerAnimated:YES completion:nil];
};
//显示编辑视图
[self presentViewController:composeController animated:YES completion:nil];
}
-(void)shareToSina{
//检查新浪微博服务是否可用
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]){
NSLog(@"新浪微博服务不可用.");
return;
}
//初始化内容编写控制器,注意这里指定分享类型为新浪微博
SLComposeViewController *composeController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
//设置默认信息
[composeController setInitialText:@"Kenshin Cui‘s Blog..."];
//添加图片
[composeController addImage:[UIImage imageNamed:@"stevenChow"]];
//添加连接
[composeController addURL:[NSURL URLWithString:@"http://www.cnblogs.com/kenshincui"]];
//设置发送完成后的回调事件
__block SLComposeViewController *composeControllerForBlock=composeController;
composeController.completionHandler=^(SLComposeViewControllerResult result){
if (result==SLComposeViewControllerResultDone) {
NSLog(@"开始发送...");
}
[composeControllerForBlock dismissViewControllerAnimated:YES completion:nil];
};
//显示编辑视图
[self presentViewController:composeController animated:YES completion:nil];
}
//苹果源生的框架只支持这几个平台,如果要分享到微信,等可以使用第三方的一些:ShareSDK,友盟等