美文网首页知识总结iosios 知识点
使用最新ShareSDK实现分享功能

使用最新ShareSDK实现分享功能

作者: 若锦 | 来源:发表于2016-07-04 17:52 被阅读2526次

将内容分享到其他平台是个非常普遍的功能,今天和大家分享一下,如何用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'

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

相关文章

  • 使用最新ShareSDK实现分享功能

    将内容分享到其他平台是个非常普遍的功能,今天和大家分享一下,如何用ShareSDK实现分享到微信好友、微信收藏、微...

  • 使用ShareSDK实现分享功能

    由于当初没有用markdown编写,文章有点乱,现在重新编写了一下,如果有需要,可以看这个使用ShareSDK实现...

  • Android分享实现总结

    前言:实现分享功能的几个办法 调用系统的分享功能 通过第三方SDK,如ShareSDK,友盟等 自行使用各自平台的...

  • ShareSDK

    使用ShareSDK授权相关功能实现用户第三方登录。流程如下所示: 1.使用ShareSDK中的hasAuthor...

  • ShareSDK分享到指定平台攻略

    本文介绍的是调用ShareSDK的内置方法实现分享到指定平台的功能,想了解ShareSDK的集成以及如何实现快捷分...

  • Sharesdk封装

    Sharesdk封装 对于分享的使用我们经常借用第三方平台来实现,这样可以高效的更新到最新的各个平台的最新的sdk...

  • Android分享---调用系统自带的分享功能

    实现分享功能的几个办法 1.调用系统的分享功能2.通过第三方SDK,如ShareSDK,友盟等3.自行使用各自平台...

  • Android分享实现总结

    前言:实现分享功能的几个办法 1.调用系统的分享功能2.通过第三方SDK,如ShareSDK,友盟等3.自行使用各...

  • Android分享---调用系统自带的分享功能

    实现分享功能的几个办法 1.调用系统的分享功能2.通过第三方SDK,如ShareSDK,友盟等3.自行使用各自平台...

  • iOS第三方SDK集成——快速集成微信、QQ、微博分享

    一直觉得使用友盟SDK和ShareSDK来实现应用的分享功能是一种懒的表现。而系统自带分享SLComposeVie...

网友评论

  • 吉s她Hmm:为什么我按照你的导入之后出现了错误 Undefined symbols for architecture x86_64:
    "_OBJC_CLASS_$_QQApiInterface", referenced from:
    objc-class-ref in AppDelegate.o
    "_OBJC_CLASS_$_ShareSDK", referenced from:
    objc-class-ref in AppDelegate.o
    "_OBJC_CLASS_$_ShareSDKConnector", referenced from:
    objc-class-ref in AppDelegate.o
    "_OBJC_CLASS_$_TencentOAuth", referenced from:
    objc-class-ref in AppDelegate.o
    "_OBJC_CLASS_$_WXApi", referenced from:
    objc-class-ref in AppDelegate.o
    "_OBJC_CLASS_$_WeiboSDK", referenced from:
    objc-class-ref in AppDelegate.o
    "_SSDKAuthTypeBoth", referenced from:
    ___57-[AppDelegate application:didFinishLaunchingWithOptions:]_block_invoke_2 in AppDelegate.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    大哥请教一下啊
    吉s她Hmm:@若锦 大神 能加我一下扣扣吗?我请求你几个问题啊 谢谢大神 870814709.。
    吉s她Hmm:@若锦 真机测试啊 Valid Architectures ????是什么?

    若锦:你的Valid Architectures设置项支持的什么呢,真机还是模拟器测试的呢
  • 念念不忘的:在吗在吗 有问题求教 :flushed:
    若锦:@就叫我阿生叔叔吧 这是因为新浪微博授权失败,你需要到微博开放平台上填一下回调地址
    念念不忘的:@若锦 shareSDK集成新浪分享(3个参数没有错),走客户端的话会出现一直redirect_uri mismatch,而走网页分享的话会成功。 :joy:
    若锦:@就叫我阿生叔叔吧 哈哈,有什么问题可以留言哟,看到了会及时回复的:stuck_out_tongue_winking_eye:
  • NateLam:要评论原来是要有队形的, 非常欣赏你的才华
    若锦: @NateLam 文青还算不上
    NateLam:@若锦 很难得,工程师还是个文青,必须要赞一下
    若锦:@NateLam 哈哈,你这队形跟的很棒 :joy: 谢谢你的夸奖
  • xiaomayi2012:非常欣赏你的才华
    若锦: @xiaomayi2012 谢谢,谬赞了。
  • Jimsir:很欣赏你的才华
    若锦:@Jimsir 谢谢夸奖 :joy:

本文标题:使用最新ShareSDK实现分享功能

本文链接:https://www.haomeiwen.com/subject/bugcjttx.html