> >

注意→_→:目前ShareSDK for iOS有2个大版本:ShareSDK v3.x(新版)和ShareSKD v2.x(旧版),本教程是ShareSDK v3.x适用的集成文档。若需要ShareSDK v2.x,请参考旧版文档:网页链接

本文档基于 ShareSDK-iOS-3.4.0 版本书写,建议搭配Unity5.x,体验快捷集成ShareSDK.
若使用 ShareSDK-iOS-3.4.0 之前版本或者 Unity 4.X 版本请参考:网页链接

1、下载ShareSDK的Unity3D工具类

打开Github下载 New-Unity-For-ShareSDK 项目,双击ShareSDKForU3DWithEditor.unitypackage导入相关文件。
注意该操作可能会覆盖您原来已经存在的文件! 

 

2、挂接ShareSDK脚本并配置平台信息

选择好需要挂接的GameObject(例如Main Camera),在右侧栏中点击Add Component,选择Share SDK 进行挂接。(如下图)

step1

 

step2

(如上图)挂接后会发现提供了当前支持的平台和及其配置信息。可以直接在此处修改你所需要的平台的配置信息。需要注意的是当前的编译环境是Android还是iOS,其字段名称是不同的哦!

红色方框第一个App Key指的是ShareSDK的appkey,可以到Mob官网注册应用获得。其他的就是各社交平台各自的配置信息。

除了可以上图处设定配置信息,也可以在ShareSDK.cs(/ Assets / Plugins / ShareSDK / ShareSDK.cs ) 和 ShareSDKDevInfo.cs(/ Assets / Plugins / ShareSDK / ShareSDKDevInfo.cs ) 文件中配置所需的平台信息,效果都是一样的,步骤如下:

i.配置您自己的ShareSDK的AppKey(在ShareSDK.cs中)

//配置ShareSDK AppKey
 #if UNITY_ANDROID
public string appKey = "moba6b6c6d6";
public string appSecret = "b89d2427a3bc7ad1aea1e1e8c1d36bf3";
#elif UNITY_IPHONE
public string appKey = "iosv1101";
public string appSecret = "";

ii.选择所需要的平台,不想要的可以直接注释或删掉(在ShareSDKDevInfo.cs中)

public class DevInfoSet

{
	public SinaWeiboDevInfo sinaweibo;
	public TencentWeiboDevInfo tencentweibo;
	public QQ qq;
	public QZone qzone;
}

iii.配置对应平台的信息(建议直接修改字符串值即可)(在ShareSDKDevInfo.cs中)

public class SinaWeiboDevInfo : DevInfo 
{
	#if UNITY_ANDROID
	public const int type = (int) PlatformType.SinaWeibo;
	public string SortId = "1";
	public string AppKey = "568898243";
	public string AppSecret = "38a4f8204cc784f81f9f0daaf31e02e3";
	public string RedirectUrl = "http://www.sharesdk.cn";
	public string ShareByAppClient = "false";
	#elif UNITY_IPHONE
	public const int type = (int) PlatformType.SinaWeibo;
	public string app_key = "568898243";
	public string app_secret ="38a4f8204cc784f81f9f0daaf31e02e3";
	public string redirect_uri = "http://www.sharesdk.cn";
	public string auth_type = "both";	//can pass "both","sso",or "web"

	#endif
}

以上平台的app_key、app_secret等字段不同分享平台可能不同,详情可参考:《统一表》。

进行分享、授权

请先引入命名空间:
using cn.sharesdk.unity3d;
使用ShareSDK:
private ShareSDK ssdk;

分享步骤:

i.定制分享信息

ShareContent content = new ShareContent();
content.SetText("this is a test string.");
content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/1.jpg");
content.SetTitle("test title");
content.SetUrl("http://www.baidu.com");
content.SetShareType(ContentType.Image);

ii:如有需要,可针对指定平台定制分享内容(可选 – 各平台定制字段请参考附件<分享内容参数表>)

ShareContent customizeShareParams = new ShareContent();
customizeShareParams.SetText("Sina share content");
customizeShareParams.SetImageUrl("http://git.oschina.net/alexyu.yxj/MyTmpFiles/raw/master/kmk_pic_fld/small/107.JPG");
customizeShareParams.SetShareType(ContentType.Image);
customizeShareParams.SetObjectID("SinaID");
//优先客户端分享
customizeShareParams.SetEnableClientShare(true);

content.SetShareContentCustomize(PlatformType.SinaWeibo, customizeShareParams);

iii.指定分享的回调

ssdk.shareHandler = ShareResultHandler;
以下为回调的定义:
void ShareResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            print ("share result :");
            print (MiniJSON.jsonEncode(result));
        }
        else if (state == ResponseState.Fail)
        {
            print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
        }
        else if (state == ResponseState.Cancel) 
        {
            print ("cancel !");
        }
    }

iv.然后将内容传入分享接口

//通过分享菜单分享
ssdk.ShowPlatformList (null, content, 100, 100);

//直接通过编辑界面分享
ssdk.ShowShareContentEditor (PlatformType.SinaWeibo, content);

//直接分享
ssdk.ShareContent (PlatformType.SinaWeibo, content);

 

授权步骤:

i.指定授权的回调

ssdk.authHandler = AuthResultHandler;
以下为回调的定义:
void AuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
	{
		if (state == ResponseState.Success)
		{
			print ("authorize success !");
		}
		else if (state == ResponseState.Fail)
		{
print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
		}
		else if (state == ResponseState.Cancel) 
		{
			print ("cancel !");
		}
	}

ii.进行授权

ssdk.Authorize(PlatformType.SinaWeibo);

 

获取用户信息

i.指定获取用户信息的回调

sdk.showUserHandler = GetUserInfoResultHandler;
以下为回调的定义:
void GetUserInfoResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
	{
		if (state == ResponseState.Success)
		{
			print ("get user info result :");
			print (MiniJSON.jsonEncode(result));
		}
		else if (state == ResponseState.Fail)
		{
			print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
}
		else if (state == ResponseState.Cancel) 
		{
			print ("cancel !");
		}
	}

ii.获取用户信息

ssdk.GetUserInfo(PlatformType.SinaWeibo);

3.修改所需要的第三方库(可选)

导入了ShareSDKForU3DWithEditor.unitypackage文件后,在ShareSDKiOSAutoPackage/Editor/SDKPorter文件夹下会找到名为SDK.zip的文件。此文件包含了完整的原生ShareSDK库以及第三方平台的库(例如新浪SDK,微信SDK等)。如果有部分第三方平台库是不需要的,可以通过解压此.zip文件,删去不需要的库,然后再次压缩即可(注意最终文件名字仍应为”SDK.zip”)。同时可以打开ShareSDK.projmods文件,针对不需要的第三方framework的路径进行删改。

4.预配置生成的Xcode项目的info.plist文件(可选)

在ShareSDKiOSAutoPackage/Editor/SDKPorter 下找到ShareSDKPostProcessBuild.cs文件,打开并在其中找到EditInfoPlist 方法,并通过修改PlistAdd字符串来修改制定URL Schme配置, 以及 LSAdd 来修改白名单。如下图:3

完成上述步骤后即可导出Xcode项目进行测试,并且不再需要在Xcode项目中进行其他操作。
Notice:如遇到任何问题,可跟我们的技术支持同事联系,QQ: 4006852216