iphone分享功能使用sharesdk分享信息到微信

步步登高 发布于 2014/03/25 09:28
阅读 9K+
收藏 3

最近接到一个项目要求分享到微信和facebook,查找网上后,推荐使用sharesdk。

1.首先到官网http://sharesdk.cn/Download下载对应的sharesdk。

2.根据官网的开发文档http://wiki.sharesdk.cn/iOS%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E6%8C%87%E5%8D%97#.E7.AC.AC.E5.85.AD.E6.AD.A5.EF.BC.9A.E5.88.86.E4.BA.AB.E5.86.85.E5.AE.B9把开发包加入到自己的工程中。

3.注意拷贝选项:

4.链接一些库文件。

6.在appdelegate类中加入重写两个方法:

 -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url wxDelegate:self];
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];
注意引入
 #import "WXApi.h" 
接下来注册sharesdk和微信share:

在方法:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
添加:

 [ShareSDK registerApp:@" 16b016b8d2b1"];
 [ShareSDK connectWeChatWithAppId:@" wxc467b1f584ded6c6" wechatCls:[WXApi class]];
 [ShareSDK importWeChatClass:[WXApi class]]; 
粉色需要到 http://sharesdk.cn/Share/#/appAdd添加引用得到的appkey。

红色需要到https://open.weixin.qq.com/添加微信的应用,得到appkey。

在需要share的地方加入如下代码:

 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"app_logo"  ofType:@"png"];
        //app_logo是图片名,png是图片格式
        //构造分享内容
        id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
                                           defaultContent:@"默认分享内容,没内容时显示"
                                                    image:[ShareSDK imageWithPath:imagePath]
                                                    title:@"ShareSDK"
                                                      url:@" http://www.sharesdk.cn"
                                              description:@"这是一条测试信息"
                                                mediaType:SSPublishContentMediaTypeNews];
        
        
        id<ISSShareActionSheetItem> wxsItem = [ShareSDK shareActionSheetItemWithTitle:[ShareSDK getClientNameWithType:ShareTypeWeixiSession]
                                                                                 icon:[ShareSDK getClientIconWithType:ShareTypeWeixiSession]
                                                                         clickHandler:^{
                                                                             [ShareSDK clientShareContent:publishContent
                                                                                                     type:ShareTypeWeixiSession
                                                                                            statusBarTips:YES
                                                                                                   result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                        if (state == SSPublishContentStateSuccess)
                                                                                                       {
                                                                                                           NSLog(NSLocalizedString(@"TEXt_SHARE_SUC", @"分享成功"));
                                                                                                       }
                                                                                                       else if (state == SSPublishContentStateFail)
                                                                                                       {
                                                                                                           NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                                                                       }
                                                                                                   }];
                                                                         }];
        //创建自定义分享列表
        NSArray *shareList = [ShareSDK customShareListWithType:
                              wxsItem,
                              nil];
        [ShareSDK showShareActionSheet:nil
                             shareList:shareList
                               content:publishContent
                         statusBarTips:YES
                           authOptions:nil
                          shareOptions: nil
                                result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                    if (state == SSResponseStateSuccess)
                                    {
                                        NSLog(@"分享成功");
                                    }
                                    else if (state == SSResponseStateFail)
                                    {
                                        NSLog(@"分享失败,错误码:%d,错误描述:%@", [error errorCode], [error errorDescription]);
                                    }
                                }]; 
接下来,需要到app-info.plist文件中加入一个URL types项(已有则不需再加),如下图

注意URL Schemes,如不添加,会提示URL Schemes的错误。

好了,运行一下,效果如图:

点击微信按钮就会跳转到微信app。

加载中
0
火蚁
火蚁
东西不错,不过应该把代码部分弄得规范一些,方便浏览阅读代码
步步登高
步步登高
谢谢,项目有点赶,没有排得很好。
0
那一抹残云
那一抹残云

在分享里面点击publish后貌似不能显示到微博上

那一抹残云
那一抹残云
@步步登高 嗯,知道原因了,sdk的key的原因,因为是网上找的跟自己的app不匹配,换了就好了
步步登高
步步登高
官方给的demo,可以分享到微博,我没做分享到微博那块。
返回顶部
顶部