shareSDK分享

将内容分享到其他平台是个非常普遍的功能,今天和大家分享一下,如何用ShareSDK实现分享到微信好友、微信收藏、微信朋友圈、QQ、QQ空间、印象笔记以及复制的功能。首先,我们需要去各个社交平台申请对应的APPKey,各个平台的网址汇总可参考:
ShareSDK各社交平台申请APPkey 的网址及申请流程汇总
。之后,我们导入ShareSDK的库。

用pod导入。

在Pod file中添加

pod 'ShareSDK3'
pod 'MOBFoundation'pod 'ShareSDK3/ShareSDKUI'
pod 'ShareSDK3/ShareSDKPlatforms/QQ'
pod 'ShareSDK3/ShareSDKPlatforms/SinaWeibo'
pod 'ShareSDK3/ShareSDKPlatforms/WeChat'

其中pod 'ShareSDK3' pod 'MOBFoundation'是必须的,其他的根据需求相应的添加,比如,如果你需要使用ShareSDK的UI,那么你就需要导入pod 'ShareSDK3/ShareSDKUI',然后需要分享到哪个社交平台就添加相应的平台。准备工作做好后我们就可以进行实现了。

1、在AppDelegate中导入头文件

#import<ShareSDK/ShareSDK.h>

#import<WXApi.h>

#import<ShareSDKConnector/ShareSDKConnector.h>

#import<TencentOpenAPI/QQApiInterface.h>

#import<TencentOpenAPI/TencentOAuth.h>

#import<WeiboSDK.h>

2、 在Appdelegate中初始化ShareSDK

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [ShareSDK registerApp:@"AppKey" activePlatforms:@[@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformSubTypeWechatTimeline),@(SSDKPlatformSubTypeWechatFav),@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeQQ),@(SSDKPlatformTypeYinXiang),@(SSDKPlatformSubTypeQZone), @(SSDKPlatformTypeCopy)] onImport:^(SSDKPlatformType platformType) {

        switch (platformType) {

            case SSDKPlatformTypeWechat: {

                [ShareSDKConnector connectWeChat:[WXApi class]];

            }

                break;

            case SSDKPlatformTypeQQ: {

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

            }

                break;

            default:

                break;

        }

    } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {

        switch (platformType) {

            case SSDKPlatformTypeSinaWeibo: {

                [appInfo SSDKSetupSinaWeiboByAppKey:@"sinaAppKey" appSecret:@"apply对应的密码" redirectUri:@"https://api.weibo.com/oauth2/default.html" authType:SSDKAuthTypeBoth];

            }

                break;

            case SSDKPlatformTypeWechat: {

                [appInfo SSDKSetupWeChatByAppId:@"微信appkey" appSecret:@"微信apply对应的密码"];

            }

                break;

            case SSDKPlatformTypeQQ: {

                [appInfo SSDKSetupQQByAppId:@"QQappID" appKey:@"QQappkey" authType:SSDKAuthTypeSSO];

            }

                break;

            case SSDKPlatformTypeYinXiang:

                [appInfo SSDKSetupEvernoteByConsumerKey:@"印象笔记appkey" consumerSecret:@"印象笔记appkey对应的密码" sandbox:NO];

                break;

            default:

                break;

        }

    }];

    return YES;
}


// 这个方法是用于从微信返回第三方App
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    [WXApi handleOpenURL:url delegate:self];

    return YES;
}

这里需要注意的一个地方是印象笔记分享中的sandbox参数,如果在测试阶段,把参数设成YES,也就是使用沙箱环境;如果项目要上传AppStore了将其改成NO.在沙箱环境下,分享成功后在印象笔记的客户端是看不到已经分享的内容的,需要到印象笔记的沙箱环境(https://sandbox.evernote.com)中查看分享的内容,只有sandbox参数为NO的时候分享成功的内容才可直接在印象笔记客户端中查看。

2.添加跳转白名单。

右击plist文件,用source code的方式打开,如图


打开plist文件

然后加入如下图所示的代码

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>mqqapi</string>
        <string>sinaweibo</string>
        <string>sinaweibohd</string>
        <string>sinaweibosso</string>
        <string>sinaweibohdsso</string>
        <string>TencentWeibo</string>
        <string>wtloginmqq2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqq</string>
        <string>weixin</string>
        <string>alipay</string>
        <string>alipayshare</string>
    </array>

或者也可以在plist文件中用key type value的方式添加,如下图


用key type value的方式添

3.将bit
![Uploading 4_740074.png . . .]code关掉


bitcode关掉

4、设置各个平台的URL Types


微信的URL Schemes

QQ 的URL Schemes

微博或者Facebook的 URL Schemes

5、添加-ObjC支持。如图


-ObjC支持

6、在需要分享的地方,实现分享方法。

NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];

分享的参数有:shareContent(分享的内容)、shareLink(分享的链接)、shareTitle(分享的标题)、netImageUrl(网络图片,给一个URL地址即可,若要分享本地图片,采用[UIImage imageNamed:@""])

1.//如果所有平台要分享的内容一致,可直接采用

[shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@%@", shareContent, [NSURL URLWithString:shareLink]]

images:netImageUrl

url:[NSURL URLWithString:shareLink]

title:shareTitle

type:SSDKContentTypeAuto];

2、如果想自定义各个平台的分享内容,则用其相应的API

// 微信朋友圈

[shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@%@", shareContent, [NSURL URLWithString:shareLink]] title:shareTitle url:[NSURL URLWithString:shareLink] thumbImage:nil image:[UIImage imageNamed:@"60x60@2x"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatSession];

// 微信收藏

[shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@", shareContent]  title:shareTitle url:nil thumbImage:nil image:[UIImage imageNamed:@"60x60@2x"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatFav];

// 微信好友

[shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@", shareContent] title:shareTitle url:[NSURL URLWithString:shareLink] thumbImage:nil image:[UIImage imageNamed:@"60x60@2x"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];

// 新浪微博,分享到微博的标题及URL需拼接到内容里,单独放在对应的参数里不好使。

[shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"【%@】%@%@",shareTitle, shareContent, [NSURL URLWithString:shareLink]] title:shareTitle image:netImageUrl url:[NSURL URLWithString:shareLink] latitude:0 longitude:0 objectID:nil type:SSDKContentTypeAuto];

// QQ好友

[shareParams SSDKSetupQQParamsByText:[NSString stringWithFormat:@"%@", shareTitle] title:nil url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformTypeQQ];

// QQ空间

[shareParams SSDKSetupQQParamsByText:[NSString stringWithFormat:@"%@", shareTitle] title:nil url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeQZone];

// 印象笔记

[shareParams SSDKSetupEvernoteParamsByText:[NSString stringWithFormat:@"【%@】%@ %@",shareTitle ,shareContent,shareLink] images:netImageUrl

title:shareTitle notebook:nil tags:nil platformType:SSDKPlatformTypeYinXiang];

// 复制

[shareParams SSDKSetupCopyParamsByText:nil images:nil url:[NSURL URLWithString:shareLink] type:SSDKContentTypeAuto];

//分享界面,items中的平台的顺序可以调整

SSUIShareActionSheetController *sheet =  [ShareSDK showShareActionSheet:nil

items:@[@(SSDKPlatformSubTypeWechatSession),

@(SSDKPlatformSubTypeWechatTimeline),

@(SSDKPlatformSubTypeWechatFav),

@(SSDKPlatformTypeSinaWeibo),

@(SSDKPlatformTypeQQ),

@(SSDKPlatformSubTypeQZone),

@(SSDKPlatformTypeYinXiang),

@(SSDKPlatformTypeCopy)]

shareParams:shareParams

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

switch (state) {

case SSDKResponseStateBegin:

break;

case SSDKResponseStateSuccess:

if (platformType == SSDKPlatformTypeCopy) {

                              NSLog(@"复制成功");

                                                                     }else{
                                                                         NSLog(@"分享成功");

                                                                     }

break;

case  SSDKResponseStateFail:

if (platformType == SSDKPlatformTypeCopy) {
                                                                        NSLog(@"复制失败");

        }else{

              NSLog(@"分享失败");

      }     

NSLog(@"失败:%@", error);

break;

default:

break;

}

}];

[sheet.directSharePlatforms addObject:@(SSDKPlatformTypeCopy)];//复制功能不用显示分享的编辑界面,所以采用直接分享的方式;若其他平台也不需要出现分享编辑的界面的话也可以直接采用此方法将对应平台加上。

最后效果如图下,点击相应的平台则可进行分享


分享菜单

shareSDK现在已经更新到3.5.2了,今天更新最新的sdk做分享的时候发现崩溃了,崩溃信息是

+[ShareSDK isClientInstalled:]: unrecognized selector sent to class 0x10fe2eb50

后面发现,新的sdk在用pod管理的时候需要添加
pod 'ShareSDK3/ShareSDKExtension'

这样点击分享按钮进行分享的时候就不会崩溃了



作者:若锦
链接:http://www.jianshu.com/p/9d3842e3d228
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值