《插件自助开发——分享类插件》

分享系统

分享系统目前采用ShareSDK的一键分享,支持新浪微博、QQ空间、微信(好友、朋友、收藏)、QQ好友、腾讯微博、人人网、易信(好友、朋友圈)的分享功能。本文介绍了如何接入SDK的分享服务。

类名和初始化方法

分享系统的类一般命名为"Share+插件名",并且需要遵循InterfaceShare协议及SDK的相关协议
以ShareSDK为例:

@interface ShareMob : NSObject <InterfaceShare,ISSShareViewDelegate>

- (void)configDeveloperInfo:(NSMutableDictionary *)cpInfo;
在init方法里调用configDeveloperInfo:方法,执行SDK的初始化操作。
cpInfo参数为字典,需要调用PluginHelper类的getParamsInfo方法,从打包工具里获取到。
以ShareSDK为例:

- (id)init {
    self = [super init];
    if (self){
       OUTPUT_LOG(@"ShareMob init!");
       [self configDeveloperInfo:[Wrapper getDeveloperInfo]];
    }
    return self;
}
- (void)configDeveloperInfo:(NSMutableDictionary *)cpInfo {
   _bDebug = false;
   OUTPUT_LOG(@"cpInfo:%@", cpInfo);
   _shareInfo = [NSMutableDictionary dictionary];
   NSString *path = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"xml"];
   NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:[NSData dataWithContentsOfFile:path]];
   [xmlParser setDelegate:self];
   [xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
   if ([elementName isEqualToString:@"ShareSDK"]) {
       [_shareInfo setObject:attributeDict forKey:@"ShareSDK"];
   } else if ([elementName isEqualToString:@"SinaWeibo"]) {
       [_shareInfo setObject:attributeDict forKey:@"SinaWeibo"];
   } else if ([elementName isEqualToString:@"QQ"]) {
       [_shareInfo setObject:attributeDict forKey:@"QQ"];
   } else if ([elementName isEqualToString:@"QZone"]) {
       [_shareInfo setObject:attributeDict forKey:@"QZone"];
   } else if ([elementName isEqualToString:@"TencentWeibo"]) {
       [_shareInfo setObject:attributeDict forKey:@"TencentWeibo"];
    } else if ([elementName isEqualToString:@"Wechat"]) {
       [_shareInfo setObject:attributeDict forKey:@"Wechat"];
    } else if ([elementName isEqualToString:@"WechatMoments"]) {
       [_shareInfo setObject:attributeDict forKey:@"WechatMoments"];
    } else if ([elementName isEqualToString:@"WechatFavorite"])  {
       [_shareInfo setObject:attributeDict forKey:@"WechatFavorite"];
    } else if ([elementName isEqualToString:@"Renren"]) {
       [_shareInfo setObject:attributeDict forKey:@"Renren"];
    } else if ([elementName isEqualToString:@"Yixin"]) {
       [_shareInfo setObject:attributeDict forKey:@"Yixin"];
    } else if ([elementName isEqualToString:@"YixinMoments"]) {
       [_shareInfo setObject:attributeDict forKey:@"YixinMoments"];
    }
 }

- (void)parserDidEndDocument:(NSXMLParser *)parser {
     OUTPUT_LOG(@"ShareSDK Dic : %@",_shareInfo);
     mAppKey = [[_shareInfo objectForKey:@"ShareSDK"] objectForKey:@"AppKey"];
     [ShareSDK registerApp:mAppKey];
     [self initializePlat:_shareInfo];
     //监听用户信息变更
     [ShareSDK addNotificationWithName:SSN_USER_INFO_UPDATE
                           target:self
                           action:@selector(userInfoUpdateHandler:)];
}

InterfaceShare

分享类插件需要实现接口InterfaceShare,接口中没有的方法,开发者可以自定义拓展。在InterfaceShare接口中,有如下函数:

public void share(Hashtable<String, String> cpInfo) //分享
public void setDebugMode(boolean paramBoolean);     // 设置调试模式(已废弃)
public String getSDKVersion();      //获取SDK 版本号
public String getPluginVersion();   //获取插件版本号

分享

- (void)share:(NSMutableDictionary *)shareInfo 功能说明:参数cpInfo包含分享相关的信息。例如需要分享的标题,文本,图片,链接等信息。
举例说明:在share方法中,我们从cpInfo中获得数据,组装成shareSDK需要的参数,调用ShareSDK的分享方法。

- (void)share:(NSMutableDictionary *)shareInfo {
   OUTPUT_LOG(@"ShareMob will share");
   OUTPUT_LOG(@"%@",shareInfo);

   NSString *content = [shareInfo objectForKey:@"text"];
   [shareInfo setObject:content forKey:@"content"];

   //wx wxsc_musicFileUrl  wxsc_extInfo wxsc_fileData wxsc_emoticonData wxhy  wxpyq
   NSString *wx_musicFileUrl = [shareInfo objectForKey:@"wx_musicFileUrl"];
   if (wx_musicFileUrl) {
      if (![shareInfo objectForKey:@"wxsc_musicFileUrl"]) {
          [shareInfo setObject:wx_musicFileUrl forKey:@"wxsc_musicFileUrl"];
       }
      if (![shareInfo objectForKey:@"wxhy_musicFileUrl"]) {
          [shareInfo setObject:wx_musicFileUrl  forKey:@"wxhy_musicFileUrl"];
       }
      if (![shareInfo objectForKey:@"wxpyq_musicFileUrl"]) {
         [shareInfo setObject:wx_musicFileUrl forKey:@"wxpyq_musicFileUrl"];
      }
   }
   NSString *wx_extInfo = [shareInfo objectForKey:@"wx_extInfo"];
   if (wx_extInfo) {
      if (![shareInfo objectForKey:@"wxsc_extInfo"]) {
         [shareInfo setObject:wx_extInfo  forKey:@"wxsc_extInfo"];
       }
      if (![shareInfo objectForKey:@"wxhy_extInfo"]) {
         [shareInfo setObject:wx_extInfo forKey:@"wxhy_extInfo"];
       }
      if (![shareInfo objectForKey:@"wxpyq_extInfo"]) {
         [shareInfo setObject:wx_extInfo forKey:@"wxpyq_extInfo"];
      }
  }
 NSString *wx_fileData = [shareInfo objectForKey:@"wx_fileData"];
   if (wx_fileData) {
      if (![shareInfo objectForKey:@"wxsc_fileData"]) {
         [shareInfo setObject:wx_fileData forKey:@"wxsc_fileData"];
      }
      if (![shareInfo objectForKey:@"wxhy_fileData"]) {
         [shareInfo setObject:wx_fileData forKey:@"wxhy_fileData"];
      }
     if (![shareInfo objectForKey:@"wxpyq_fileData"]) {
        [shareInfo setObject:wx_fileData forKey:@"wxpyq_fileData"];
     }
  }
  NSString *wx_emoticonData = [shareInfo  objectForKey:@"wx_emoticonData"];
  if (wx_emoticonData) {
    if (![shareInfo objectForKey:@"wxsc_emoticonData"]) {
        [shareInfo setObject:wx_emoticonData forKey:@"wxsc_emoticonData"];
    }
    if (![shareInfo objectForKey:@"wxhy_emoticonData"]) {
        [shareInfo setObject:wx_emoticonData forKey:@"wxhy_emoticonData"];
    }
    if (![shareInfo objectForKey:@"wxpyq_emoticonData"]) {
        [shareInfo setObject:wx_emoticonData forKey:@"wxpyq_emoticonData"];
    }
}

 OUTPUT_LOG(@"shareInfo:%@", shareInfo);

 [self shareAllButtonClickHandler:shareInfo];
}

-(void)shareAllButtonClickHandler:(NSMutableDictionary *)  shareInfo {
   NSString *img_name = [shareInfo objectForKey:@"image"]; //IMAGE_NAME
//    NSString *img_type = @"png"; //IMAGE_EXT
   NSString *img_thumb = [shareInfo objectForKey:@"thumbImage"];
   NSString *imagePath = [[NSBundle mainBundle] pathForResource:img_name ofType:nil inDirectory:@""];

//构造分享内容
id<ISSContent> publishContent = [ShareSDK content:[shareInfo objectForKey:@"content"]
                                   defaultContent:[shareInfo objectForKey:@"defaultContent"]
                                            image:[ShareSDK imageWithUrl:imagePath]
                                            title:[shareInfo objectForKey:@"title"]
                                              url:[shareInfo objectForKey:@"url"]
                                      description:[shareInfo objectForKey:@"description"]
                                        mediaType:[[shareInfo objectForKey:@"mediaType"] intValue]];

//以下信息为特定平台需要定义分享内容,如果不需要可省略下面的添加方法

//定制人人网信息
if (isUseRenRen) {
    [publishContent addRenRenUnitWithName:INHERIT_VALUE
                              description:INHERIT_VALUE
                                      url:INHERIT_VALUE
                                  message:INHERIT_VALUE
                                    image:INHERIT_VALUE
                                  caption:[shareInfo objectForKey:@"renren_caption"]];
}

//定制QQ空间信息
if (isUseQZone) {
    [publishContent addQQSpaceUnitWithTitle:INHERIT_VALUE
                                        url:INHERIT_VALUE
                                       site:[shareInfo objectForKey:@"qq_site"]
                                    fromUrl:[shareInfo objectForKey:@"qq_fromUrl"]
                                    comment:INHERIT_VALUE
                                    summary:INHERIT_VALUE
                                      image:INHERIT_VALUE
                                       type:[self nstr2nsnum:[shareInfo objectForKey:@"qq_type"]]
                                    playUrl:[shareInfo objectForKey:@"qq_playUrl"]
                                       nswb:[self nstr2nsnum:[shareInfo objectForKey:@"qq_nswb"]]];
}

//定制微信好友信息
if (isUseWeixinSession) {
    [publishContent addWeixinSessionUnitWithType:INHERIT_VALUE
                                         content:INHERIT_VALUE
                                           title:INHERIT_VALUE
                                             url:INHERIT_VALUE
                                      thumbImage:[ShareSDK imageWithUrl:img_thumb]
                                           image:INHERIT_VALUE
                                    musicFileUrl:[shareInfo objectForKey:@"wxhy_musicFileUrl"]
                                         extInfo:[shareInfo objectForKey:@"wxhy_extInfo"]
                                        fileData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"wxhy_fileData"]]
                                    emoticonData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"wxhy_emoticonData"]]];
}

//定制微信朋友圈信息
if (isUseWeixinTimeline) {
    [publishContent addWeixinTimelineUnitWithType:INHERIT_VALUE
                                          content:INHERIT_VALUE
                                            title:INHERIT_VALUE
                                              url:INHERIT_VALUE
                                       thumbImage:[ShareSDK imageWithUrl:img_thumb]
                                            image:INHERIT_VALUE
                                     musicFileUrl:[shareInfo objectForKey:@"wxpyq_musicFileUrl"]
                                          extInfo:[shareInfo objectForKey:@"wxpyq_extInfo"]
                                         fileData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"wxpyq_fileData"]]
                                     emoticonData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"wxpyq_emoticonData"]]];
}

//定制微信收藏信息
if (isUseWeixinFav) {
    [publishContent addWeixinFavUnitWithType:INHERIT_VALUE
                                     content:INHERIT_VALUE
                                       title:INHERIT_VALUE
                                         url:INHERIT_VALUE
                                  thumbImage:[ShareSDK imageWithUrl:img_thumb]
                                       image:INHERIT_VALUE
                                musicFileUrl:[shareInfo objectForKey:@"wxsc_musicFileUrl"]
                                     extInfo:[shareInfo objectForKey:@"wxsc_extInfo"]
                                    fileData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"wxsc_fileData"]]
                                emoticonData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"wxsc_emoticonData"]]];
}

//定制QQ分享信息
if (isUseQQ) {
    [publishContent addQQUnitWithType:INHERIT_VALUE
                              content:[shareInfo objectForKey:@"content"]
                                title:[shareInfo objectForKey:@"title"]
                                  url:[shareInfo objectForKey:@"titleUrl"]
                                image:[ShareSDK imageWithUrl:[shareInfo objectForKey:@"imageUrl"]]];
}


//定制易信好友信息
if (isUseYiXin) {
    [publishContent addYiXinSessionUnitWithType:INHERIT_VALUE
                                        content:INHERIT_VALUE
                                          title:INHERIT_VALUE
                                            url:INHERIT_VALUE
                                     thumbImage:[ShareSDK imageWithUrl:img_thumb]
                                          image:INHERIT_VALUE
                                   musicFileUrl:[shareInfo objectForKey:@"yxhy_musicFileUrl"]
                                        extInfo:[shareInfo objectForKey:@"yxhy_extInfo"]
                                       fileData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"yxhy_fileData"]]];
}

//定义易信朋友圈信息
if (isUseYiXinTimeline) {
    [publishContent addYiXinTimelineUnitWithType:INHERIT_VALUE
                                         content:INHERIT_VALUE
                                           title:INHERIT_VALUE
                                             url:INHERIT_VALUE
                                      thumbImage:[ShareSDK imageWithUrl:img_thumb]
                                           image:INHERIT_VALUE
                                    musicFileUrl:[shareInfo objectForKey:@"yxpyq_musicFileUrl"]
                                         extInfo:[shareInfo objectForKey:@"yxpyq_extInfo"]
                                        fileData:[[NSData alloc] initWithContentsOfFile:[shareInfo objectForKey:@"yxpyq_fileData"]]];
}

if ([[UIDevice currentDevice].systemVersion versionStringCompare:@"7.0"] != NSOrderedAscending)
{
    //7.0以上只允许发文字,定义Line信息
    [publishContent addLineUnitWithContent:INHERIT_VALUE
                                     image:nil];
}

//结束定制信息

//创建弹出菜单容器
id<ISSContainer> container = [ShareSDK container];
[container setIPadContainerWithView:_sender arrowDirect:UIPopoverArrowDirectionUp];

id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                     allowCallback:NO
                                                     authViewStyle:SSAuthViewStyleFullScreenPopup
                                                      viewDelegate:nil
                                           authManagerViewDelegate:self];

//在授权页面中添加关注官方微博
[authOptions setFollowAccounts:[NSDictionary dictionaryWithObjectsAndKeys:
                                [ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"],
                                SHARE_TYPE_NUMBER(ShareTypeSinaWeibo),
                                [ShareSDK userFieldWithType:SSUserFieldTypeName value:@"ShareSDK"],
                                SHARE_TYPE_NUMBER(ShareTypeTencentWeibo),
                                nil]];

id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:[shareInfo objectForKey:@"title"]
                                                          oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                           qqButtonHidden:YES
                                                    wxSessionButtonHidden:YES
                                                   wxTimelineButtonHidden:YES
                                                     showKeyboardOnAppear:NO
                                                        shareViewDelegate:self
                                                      friendsViewDelegate:self
                                                    picViewerViewDelegate:nil];

OUTPUT_LOG(@"_shareList:%@",_shareList);
//弹出分享菜单
[ShareSDK showShareActionSheet:container
                     shareList:_shareList
                       content:publishContent
                 statusBarTips:YES
                   authOptions:authOptions
                  shareOptions:shareOptions
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {

                            if (state == SSResponseStateSuccess)
                            {
                                OUTPUT_LOG(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                [ShareWrapper onShareResult:self retCode:SHARERESULT_SUCCESS retMsg:@"TEXT_ShARE_SUCCESS"];
                            }
                            else if (state == SSResponseStateFail)
                            {
                                OUTPUT_LOG(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                [ShareWrapper onShareResult:self retCode:SHARERESULT_FAIL retMsg:@"TEXT_ShARE_FAID"];
                            }
                        }];
}

分享参数如下表

参数 是否必传 参数说明
title Y title标题,在印象笔记、邮箱、信息、微信(包括好友、朋友圈和收藏)、 易信(包括好友、朋友圈)、人人网和QQ空间使用
titleUrl Y titleUrl是标题的网络链接,仅在人人网和QQ空间使用
site Y site是分享此内容的网站名称,仅在QQ空间使用
siteUrl Y siteUrl是分享此内容的网站地址,仅在QQ空间使用
text N text是分享文本,所有平台都需要这个字段
imagePath N imagePath是本地的图片路径,除Linked-In外的所有平台都支持这个字段
imageUrl N imageUrl是图片的网络路径,新浪微博、人人网、QQ空间和Linked-In支持此字段
dialogMode N 编辑分享内容的模式,默认全屏false,对话框为true
comment N comment是我对这条分享的评论,仅在人人网和QQ空间使用
url N 在微信(包括好友、朋友圈收藏)和易信(包括好友和朋友圈)中使用
theme N 设置主题,默认为传统主题,可传"skyblue"修改主题
latitude N 分享地纬度,新浪微博、腾讯微博和foursquare支持此字段
longitude N 分享地经度,新浪微博、腾讯微博和foursquare支持此字段

在share方法中,我们在ShareSDK的PlatformActionListener监听中将分享结果通知CP,以下是分享的回调说明。

回调信息 code msg
分享成功 SHARERESULT_SUCCESS null或者错误信息的简单描述
分享取消 SHARERESULT_CANCEL null或者错误信息的简单描述
分享失败 SHARERESULT_FAIL null或者错误信息的简单描述
分享网络出现错误 SHARERESULT_NETWORK_ERROR null或者错误信息的简单描述

设置Debug模式

- (void)setDebugMode:(BOOL)debug;
已废弃,实现方式参照模板。

获取SDK版本号

- (NSString *)getSDKVersion;
SDK版本是指第三方SDK的版本号,格式可能都不一样,如6.0、1.0.3、2.1.0.4等,这个版本号格式不需要任何改动。

获取插件版本号

- (NSString *)getPluginVersion;
格式『版本序号_SDK版本号』,版本序号从『2.0.0』开始,每次更新+0.0.1