> >

ShareSDK iOS(v2.x)for JavaScript

1、通用集成步骤

1.1、下载ShareSDK for JS 组件

打开Github下载JSSample项目。将JSApi/ShareSDK.js拷贝到Web页面可以访问的路径(现在不支持浏览器打开的web应用,只支持App内置的Web页面,这个web页可以自己在本地也就是项目里创建html文件,这个html文件也可以网上得到)。

1.2、导入ShareSDK.js脚本

在Web页中加入ShareSDK.js脚本。如:

<script language="javascript" type="text/javascript" src="ShareSDK.js"></script>

初始化SDK

<!DOCTYPE html>
<html>
<head>
    <title>ShareSDK for JS Sample</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <script language="javascript" type="text/javascript" src="ShareSDK.js"></script>
    <script language="javascript" type="text/javascript">

        function init()
        {
            $sharesdk.open("iosv1101", true);
}
</script>
</head>
<body onload="init()" style="text-align: center"></body></html>

可以在页面onload事件中调用open方法进行SDK初始化。代码如下(其他平台里初始化的参数可以参照这里):

function init()
 {
        $sharesdk.open("iosv1101", true);

        var sinaConf = {};
        sinaConf["app_key"] = "568898243";
        sinaConf["app_secret"] = "38a4f8204cc784f81f9f0daaf31e02e3";
        sinaConf["redirect_uri"] = "http://www.sharesdk.cn";
        $sharesdk.setPlatformConfig($sharesdk.platformID.SinaWeibo, sinaConf);
}

1.3、分享内容

先构造分享内容结构,代码如下:

var params = {"text" : "测试的文字",
"imageUrl" : "http://img0.bdstatic.com/img/image/shouye/tangwei.jpg",
"title" : "测试的标题","titleUrl" : "http://sharesdk.cn","description" : "测试的描述",
"site" : "ShareSDK","siteUrl" : "http://sharesdk.cn",
"type" : $sharesdk.contentType.Text};

然后将内容传入分享接口

  • 弹出分享菜单
$sharesdk.shareContent($sharesdk.platformID.SinaWeibo, params, function (platform, state, shareInfo, error) {alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);});
  • 弹出分享编辑界面
$sharesdk.showShareView($sharesdk.platformID.SinaWeibo, params, function (platform, state, shareInfo, error) {alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);});
  • 直接分享
$sharesdk.shareContent($sharesdk.platformID.SinaWeibo, params, function (platform, state, shareInfo, error) {

alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);

});
  • 一键分享
$sharesdk.oneKeyShareContent([$sharesdk.platformID.SinaWeibo, $sharesdk.platformID.TencentWeibo], params, function (platform, state, shareInfo, error) {

alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error);

});
关于具体的分享参数名称,可以查看下载SDK里的ShareSDK.JS文件里的配置;

1.4、用户授权

调用ShareSDK中的authorize方法可进行用户授权。代码如下:

$sharesdk.authorize($sharesdk.platformID.SinaWeibo, function (platform, state, error){

alert("state = " + state + "\nerror = " + error);

});

获取用户资料

调用ShareSDK中的getUserInfo来获取授权用户的信息。代码如下:

$sharesdk.getUserInfo($sharesdk.platformID.SinaWeibo, function (platform, state, user, error) {

alert("state = " + state + "\nuser = " + user + "\nerror = " + error);

});

2、iOS部分集成步骤

导入JS桥接的iOS版本组件

打开下载的JS包里的iOS/ShareSDKForJS/ShareSDKForJS.xcodeproj项目工程,选择ShareSDKForJS静态库运行Xcode,如图:

700px-js_doc_001.png

运行成功之后,请大家打开文件找到Build文件夹,然后进入文件中找到build/Products/Debug-iphonesimulato /libShareSDKForJS.a文件以及找到build/Products/Debug-iphonesimulator/usr/local /include/ShareSDKJSBridge.h这个文件,再将libShareSDKForJS.a和ShareSDKJSBridge.h这 2个文件导入到你的项目里,如图

700px-js_doc_002.png

注意:(如果在分享中弹不出分享的菜单,如果大家的ShareSDK.js文件是本地的,要把这个加到Build Phases里面的Copy Bundle Resources选项)

下载iOS版本ShareSDK,并导入到工程中。

登录ShareSDK的官方网站下载并解压最新版本的SDK。如果您还尚未下载,请 点击这里下载 或者访问 http://sharesdk.cn/Download 。解压后如图所示:

700px-js_doc_003

将ShareSDK导入工程中,并添加相应的依赖库。此步骤可以参考:《iOS快速集成指南》中的第一、第二步。执行完成后,如下图所示:

700px-js_doc_004.png

为UIWebView添加消息捕获

先导入ShareSDKJSBridge.h头文件。

#import "ShareSDKJSBridge.h"

然后给UIWebView设置一个委托对象,如:

webView.delegate = self;
注意:webView是UIWebView的对象实例。

最后实现shouldStartLoadWithRequest委托方法,并在方法中加入JSBridge的captureRequest方法,代码如下:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return ![[ShareSDKJSBridge sharedBridge] captureRequest:request webView:webView];
}

添加完毕后就可以对工程进行运行测试。