如何实现社交分享:
在iOS中实现社交分享的方法有三种:
->自己编写各个平台的分享代码(代码量较多)
->利用ios自带的Social.framework
->利用第三方的分享框架
ShareSDK:http://wiki.mob.com/iOS快速集成指南
百度社会化分享组件:http://developer.baidu.com/soc/share
(百度还有个“社会化登录组件”:http://developer.baidu.com/soc/login)
Social.framework
Social.framework
支持的分享平台(打开手机上的“设置”即可看到)
使用Social.framework之前得在“设置”中添加相应分享平台的帐号
示例:
#import "ViewController.h"
#import <Social/Social.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.判断平台是否可用
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
NSLog(@"查看您是否设置了新浪微博帐号,设置界面-->新浪微博-->配置帐号");
}
// 2.创建SLComposeViewController
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
// 2.1.添加分享文字
[composeVc setInitialText:@"做人如果没有梦想,跟咸鱼有什么区别"];
// 2.2.添加分享图片
[composeVc addImage:[UIImage imageNamed:@"xingxing"]];
// 3.弹出分享界面
[self presentViewController:composeVc animated:YES completion:nil];
// 4.设置block属性(监听用户是点击了取消分享还是分享按钮)
composeVc.completionHandler = ^ (SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"用户点击了取消");
} else {
NSLog(@"用户点击了发送");
}
};
}
@end
友盟分享
官网:http://bbs.umeng.com/forum-social-1.html
AppDelegate:
oauth2.0授权
弹出个网页让用户输入账号密码授权
sso授权:
直接跳转到手机上的新浪微博应用中,如果以前登陆过的话直接授权,然后跳回去
SSO - 目前在国内使用比较多,如果本机安装了某个应用程序,会直接进入该应用程序获得授权。
URL Schemes填"sina."+你的友盟AppKey.
下面以分享到新浪微博平台为例:
从网上下载友盟的SDK,然后导包
</pre></p><pre name="code" class="objc"><span style="font-size:18px;"></span>
<span style="font-size:18px;">#import "AppDelegate.h"
#import "UMSocial.h"
#import "UMSocialSinaHandler.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// appKey
[UMSocialData setAppKey:@"54ced694fd98c588d2000210"];
// sso授权地址
[UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSLog(@"%@", url);
return [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [UMSocialSnsService handleOpenURL:url];
}
@end
</span>
ViewController:
<span style="font-size:18px;">#import "ViewController.h"
#import "UMSocial.h"
@interface ViewController ()
- (IBAction)shareClick;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)shareClick {
NSString *shareText = @"出任CEO,赢取白富美,走向人生巅峰";
UIImage *shareImage = [UIImage imageNamed:@"xingxing"];
// [UMSocialSnsService presentSnsIconSheetView:self
// appKey:@"54ced694fd98c588d2000210"
// shareText:shareText
// shareImage:shareImage
// shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil]
// delegate:nil]; // 分享平台
[UMSocialSnsService presentSnsController:self appKey:@"54ced694fd98c588d2000210" shareText:shareText shareImage:shareImage shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil] delegate:nil];
}
@end
</span>