使用shareSDk进行第三方分享

一、下载导入

1.进入mob官网(mob.com)下载自己需要的shareSDK。

2.将下载后的文件(.tar.gz)格式改为zip格式后,解压。

3.将文件导入项目中。

二、项目配置

1.添加依赖库(具体参照官网添加依赖库步骤)

使用shareSDk进行第三方分享_第1张图片

2.添加shareSDK的AppKey,以及实现代码。

在appdelegate.m中

(1)导入类库

//第三方分享

#import

#import

//腾讯开放平台(对应QQ和QQ空间)SDK头文件

#import

#import

//微信SDK头文件

#import "WXApi.h"

//新浪微博SDK头文件

#import "WeiboSDK.h"

//新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"


(2)在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中实现的代码

[ShareSDK registerApp:@"1d7970589ec14" activePlatforms:@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformSubTypeQZone),@(SSDKPlatformTypeSMS),@(SSDKPlatformTypeCopy),//@(SSDKPlatformSubTypeWechatSession),//@(SSDKPlatformSubTypeWechatTimeline),@(SSDKPlatformSubTypeQQFriend),@(SSDKPlatformSubTypeWechatFav)]  

 onImport:^(SSDKPlatformType platformType)

{

switch (platformType)

{

case SSDKPlatformTypeWechat:

[ShareSDKConnector connectWeChat:[WXApi class]];

break;

case SSDKPlatformTypeQQ:

[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];

break;

case SSDKPlatformTypeSinaWeibo:

[ShareSDKConnector connectWeibo:[WeiboSDK class]];

break;

case SSDKPlatformSubTypeQZone:

break;

case SSDKPlatformTypeSMS:

break;

case SSDKPlatformTypeCopy:

break;

//case SSDKPlatformSubTypeWechatSession:

//break;

//case SSDKPlatformSubTypeWechatTimeline:

//break;

case SSDKPlatformSubTypeQQFriend:

break;

case SSDKPlatformSubTypeWechatFav:

break;

default:

break;

}

}

onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)

{

switch (platformType)

{

case SSDKPlatformTypeSinaWeibo:

//设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权

[appInfo SSDKSetupSinaWeiboByAppKey:@"979793039" appSecret:@"146adc5f73eced0654b711bdb4b18cac" redirectUri:@"http://msskd.com"  authType:SSDKAuthTypeBoth];

break;

case SSDKPlatformTypeWechat:

[appInfo SSDKSetupWeChatByAppId:@"wx8970561dd448bd02"

appSecret:@""];

break;

case SSDKPlatformTypeQQ:

[appInfo SSDKSetupQQByAppId:@"1106058709" appKey:@"L1Oz6KYzx2F6nHGl" authType:SSDKAuthTypeBoth];

default:

break;

}

}];

(3)在实现分享页面点击按钮方法中代码

[1]导入shareSDK的类库

#import

#import

[2]使用系统UI界面实现代码

//1、创建分享参数

NSArray* imageArray = @[[UIImage imageNamed:@"appIcon.png"]];

//(注意:图片必须要在Xcode左边目录里面,名称必须要传正确,如果要分享网络图片,可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])

if (imageArray)

{

NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];

//允许客户端进行分享

[shareParams SSDKEnableUseClientShare];

//自动设置的分享内容

//[shareParams SSDKSetupShareParamsByText:@"分享内容"  images:imageArray  url:[NSURL URLWithString:@"http://msskd.com"] title:@"分享标题" type:SSDKContentTypeWebPage];

//单独设置的不同平台分享内容

[shareParams SSDKSetupQQParamsByText:@"同城-专人-直送-4元起! 代取、代送、代买、代办事,顺风件~  微信公众号-app均可下单" title:@"郑州人都在用的跑腿APP" url:[NSURL URLWithString:userQRCodeString] thumbImage:[UIImage imageNamed:@"appIcon.png"] image:[UIImage imageNamed:@"appIcon.png"] type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQZone];

[shareParams SSDKSetupQQParamsByText:@"同城-专人-直送-4元起! 代取、代送、代买、代办事,顺风件~  微信公众号-app均可下单" title:@"郑州人都在用的跑腿APP" url:[NSURL URLWithString:userQRCodeString] thumbImage:[UIImage imageNamed:@"appIcon.png"] image:[UIImage imageNamed:@"appIcon.png"] type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeQQFriend];

[shareParams SSDKSetupWeChatParamsByText:@"同城-专人-直送-4元起! 代取、代送、代买、代办事,顺风件~  微信公众号-app均可下单" title:@"郑州人都在用的跑腿APP" url:[NSURL URLWithString:userQRCodeString] thumbImage:[UIImage imageNamed:@"appIcon.png"] image:[UIImage imageNamed:@"appIcon.png"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeWebPage forPlatformSubType:SSDKPlatformSubTypeWechatFav];

//[self startShareWithType:SSDKPlatformTypeSinaWeibo];

//[self startShareWithType:SSDKPlatformSubTypeWechatSession];

//[self startShareWithType:SSDKPlatformSubTypeWechatTimeline];

//[self startShareWithType:SSDKPlatformSubTypeQQFriend];

[shareParams SSDKSetupSinaWeiboShareParamsByText:@"同城-专人-直送-4元起! 代取、代送、代买、代办事,顺风件~  微信公众号-app均可下单" title:@"郑州人都在用的跑腿APP" image:[UIImage imageNamed:@"appIcon.png"] url:[NSURL URLWithString:userQRCodeString] latitude:0 longitude:0 objectID:nil type:SSDKContentTypeWebPage];

//2、分享(可以弹出我们的分享菜单和编辑界面)

[ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响

items:@[@(SSDKPlatformTypeSinaWeibo),

@(SSDKPlatformSubTypeQZone),

//@(SSDKPlatformTypeSMS),

//@(SSDKPlatformTypeCopy),

//@(SSDKPlatformSubTypeWechatSession),

//@(SSDKPlatformSubTypeWechatTimeline),

@(SSDKPlatformSubTypeQQFriend),

@(SSDKPlatformSubTypeWechatFav)]

shareParams:shareParams

onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {

switch (state) {

case SSDKResponseStateSuccess:

{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];

[alertView show];

break;

}

case SSDKResponseStateFail:

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

break;

}

default:

break;

}

}

];

}

[3]使用自定义UI界面实现代码

其中shareType为平台类型,UI自己实现。

[ShareSDK share:shareType parameters:publishContent onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {

switch (state) {

case SSDKResponseStateSuccess:

{

NSLog(@"分享成功!");

[self showAlertViewWithTitle:@"分享成功" message:nil];

break;

}

case SSDKResponseStateFail:

{

NSLog(@"分享失败!");

[self showAlertViewWithTitle:@"分享失败" message:nil];

break;

}

case SSDKResponseStateCancel:

{

[self showAlertViewWithTitle:@"取消分享" message:nil];

break;

}

default:

break;

}

}];

3.适配iOS9+(官方文档都有详细信息)

大部分社交平台接口不支持https协议

小部分社交平台SDK不支持bitcode

添加白名单Schemes(在info.plist中设置)

使用shareSDk进行第三方分享_第2张图片

添加URL types

URL Schemes内容为:

各平台缩写+appID

其中QQ+appID的16进制(必须是大写字母,且不够8为数的,前面添加0)

使用shareSDk进行第三方分享_第3张图片

以上均为大概信息步骤,具体步骤以及代码看官方文档,官方还是写的比较详细。我这里只是用作自我总结,加深记忆。

补充问题:

1.开发中使用shareSDk的UI界面,新浪微博不能直接跳转app,先跳出shareSDK的弹窗,完善信息后才能跳转。(shareSDK的弹窗的右边图片默认是变型的,但是不影响分享后的图片,介于美观考虑,不推荐使用默认分享UI)。

你可能感兴趣的:(使用shareSDk进行第三方分享)