起因
因为要更新老项目中的一个远古时期的友盟分享SDK,用CocoaPods导入发现版本太低报错,以及最新版SDK使用方法大变,封装了两个统一的方法便于项目中的替换。下面先说CocoaPods中的升级与坑。
主要内容
- CocoaPods升级与坑
- 最新版友盟的封装
CocoaPods升级与坑
CocoaPods升级网上有N多个版本,无外乎,一下操作流程,替换镜像然后安装,2017最新的镜像源一般替换为以下镜像
https://gems.ruby-china.org
如果替换ruby-china之后仍然报错如下
ERROR: While executing gem ... (OpenSSL::SSL::SSLError)
hostname "upyun.gems.ruby-china.org" does not match the server certificate
建议更换阿里云的试一下
http://rubygems-china.oss.aliyuncs.com
CocoaPods升级完整步骤
- 更新新的gem
sudo gem update --system
- 查看现有镜像
gem sources -l
- 移除镜像
sudo gem sources --remove https://rubygems.org/
sudo gem sources -remove https://gems.ruby-china.org/
- 添加新的镜像
sudo gem source -a http://rubygems-china.oss.aliyuncs.com
- 查看镜像
gem sources -l
ygkjdeMac-mini:~ ygkj$ gem sources -l
*** CURRENT SOURCES ***
http://rubygems-china.oss.aliyuncs.com
- 安装
sudo gem install cocoapods
Fetching: activesupport-4.2.10.gem (100%)
Successfully installed activesupport-4.2.10
Fetching: cocoapods-core-1.3.1.gem (100%)
Successfully installed cocoapods-core-1.3.1
Fetching: claide-1.0.2.gem (100%)
Successfully installed claide-1.0.2
Fetching: cocoapods-downloader-1.1.3.gem (100%)
Successfully installed cocoapods-downloader-1.1.3
Fetching: netrc-0.11.0.gem (100%)
Successfully installed netrc-0.11.0
Fetching: cocoapods-trunk-1.3.0.gem (100%)
Successfully installed cocoapods-trunk-1.3.0
Fetching: molinillo-0.5.7.gem (100%)
Successfully installed molinillo-0.5.7
。。。。。。
- 查看版本
pod --version
- 更新当前导入SDK (更新现有的版本 只更新当前版本 不改变之前导入的其他SDK的版本)
pod install --verbose --no-repo-update
新版友盟使用
参照官方文档设置appkey和回调代码之后,根据项目需要封装了两个方法:
根据项目中最常用的两种分享方式设置了一个枚举如下:
typedef NS_ENUM(NSUInteger,umSocialShareType) {
UMS_SHARE_TYPE_IMAGE = 0,//只分享图片
UMS_SHARE_TYPE_ALL = 1,//图片文字链接
};
方法1.点击需要分享的地方弹出多个分享平台,然后选择具体的平台进行分享。
根据不同的枚举值确定点击的某个平台的分享类型,然后传入不同的数据参数。
/**
* 弹出多个平台 选择分享至某一个
* sharePlatType 分享的类型 图片模式 还是图文链接模式
* shareTitle 分享的标题 图片模式时可传nil
* shareCotent 分享的内容 图片模式时可传nil
* shareImage 分享的图片 UIImage类对象,也可以是NSdata类对象,也可以是图片链接imageUrl NSString类对象
* shareWebUrl 分享点击对应的web页的URL 图片模式时可传nil
* @param block 回调block
*/
- (void)umSocial_ShareWithControll:(UIViewController *)PrsentControll withPlatShareType:(umSocialShareType)shareType withTitle:(NSString*)shareTitle withCotent:(NSString*)shareCotent withImage:(id)shareImage withWebUrl:(NSString*)shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler;
方法的实现如下:
/**
* 弹出多个平台 选择分享至某一个
* sharePlatType 分享的类型 图片模式 还是图文链接模式
* @param block 回调block
*/
- (void)umSocial_ShareWithControll:(UIViewController *)PrsentControll withPlatShareType:(umSocialShareType)shareType withTitle:(NSString*)shareTitle withCotent:(NSString*)shareCotent withImage:(id)shareImage withWebUrl:(NSString*)shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler{
switch (shareType) {
case UMS_SHARE_TYPE_IMAGE://只分享图片
{
[UMSocialUIManager setPreDefinePlatforms:platFormArray];
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
[self runShareImageWithType:platformType WithControll:PrsentControll withImage:shareImage withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler ];
}];
}
break;
case UMS_SHARE_TYPE_ALL://图片文字链接
{
[UMSocialUIManager setPreDefinePlatforms:platFormArray];
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
[self runShareAllWithType:platformType WithControll:PrsentControll withTitle:shareTitle withCotent:shareCotent withImage:shareImage withWebUrl:shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler ];
}];
}
break;
default:
break;
}
}
方法2.点击需要分享的地方传入相关的拼团类型,直接分享到目的地平台。
/**
* 分享到某一平台 不弹出框
* shareType 分享的类型 图片模式 还是图文链接模式
* type 直接送达的平台 UMSocialPlatformType内包含的平台
* shareTitle 分享的标题 图片模式时可传nil
* shareCotent 分享的内容 图片模式时可传nil
* shareImage 分享的图片 UIImage类对象,也可以是NSdata类对象,也可以是图片链接imageUrl NSString类对象
* shareWebUrl 分享点击对应的web页的URL 图片模式时可传nil
* @param block 回调block
*/
-(void)umSocial_ShareWithControll:(UIViewController *)PrsentControll withOutPlatShareType:(umSocialShareType)shareType withPostType:(UMSocialPlatformType)type withTitle:(NSString*)shareTitle withCotent:(NSString*)shareCotent withImage:(id)shareImage withWebUrl:(NSString*)shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler;
方法实现如下:
/**
* 分享到某一平台 不弹出框
* shareType 分享的类型 图片模式 还是图文链接模式
* @param block 回调block
*/
-(void)umSocial_ShareWithControll:(UIViewController *)PrsentControll withOutPlatShareType:(umSocialShareType)shareType withPostType:(UMSocialPlatformType)type withTitle:(NSString*)shareTitle withCotent:(NSString*)shareCotent withImage:(id)shareImage withWebUrl:(NSString*)shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler{
switch (shareType) {
case UMS_SHARE_TYPE_IMAGE://只分享图片 不弹出平台
{
[self runShareImageWithType:type WithControll:PrsentControll withImage:shareImage withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler ];
}
break;
case UMS_SHARE_TYPE_ALL://图片文字链接 不弹出平台
{
[self runShareAllWithType:type WithControll:PrsentControll withTitle:shareTitle withCotent:shareCotent withImage:shareImage withWebUrl:shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler ];
}
break;
default:
break;
}
}
调取真正执行分享的公共方法
不管是上述那个方法中共同调用图片模式分享和图文链接模式的分享对应的公共方法,在图文链接分享的公共方法中区分新浪微博的特殊形式。
//只分享图片
-(void)runShareImageWithType:(UMSocialPlatformType)type WithControll:(UIViewController *)PrsentControll withImage:(id)shareImage withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler{
if (shareImage == nil || [NSString stringWithFormat:@"%@",shareImage].length <1) {
[SVProgressHUD showInfoWithStatus:@"图片信息不能为空"];
return;
}
UMSocialMessageObject *messageObj = [UMSocialMessageObject messageObject];
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
[shareObject setShareImage:shareImage];
messageObj.shareObject = shareObject;
[[UMSocialManager defaultManager]shareToPlatform:type messageObject:messageObj currentViewController:PrsentControll completion:^(id result, NSError *error) {
if (error)
{
completionHandler(nil,error);
}else{
completionHandler(result,nil);
}
}];
}
//图片文字链接
-(void)runShareAllWithType:(UMSocialPlatformType)type WithControll:(UIViewController *)PrsentControll withTitle:(NSString*)shareTitle withCotent:(NSString*)shareCotent withImage:(id)shareImage withWebUrl:(NSString*)shareWebUrl withCompletionHandler:(UMSocialRequestCompletionHandler)completionHandler{
UMSocialMessageObject *messageObj = [UMSocialMessageObject messageObject];
if (type == UMSocialPlatformType_Sina) //如果是渣浪
{
if (shareImage == nil || [NSString stringWithFormat:@"%@",shareImage].length <1) {
[SVProgressHUD showInfoWithStatus:@"图片信息不能为空"];
return;
}
messageObj.text = [NSString stringWithFormat:@"%@%@%@",shareTitle,shareCotent,shareWebUrl];
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
[shareObject setShareImage:shareImage];
messageObj.shareObject = shareObject;
}else
{
if (shareTitle.length <1 || shareWebUrl.length <1) {
[SVProgressHUD showInfoWithStatus:@"检查标题和网址不能为空"];
return;
}
UMShareWebpageObject *shareObj =[UMShareWebpageObject shareObjectWithTitle:shareTitle descr:shareCotent thumImage:shareImage];
shareObj.webpageUrl = shareWebUrl;
messageObj.shareObject = shareObj;
}
[[UMSocialManager defaultManager]shareToPlatform:type messageObject:messageObj currentViewController:PrsentControll completion:^(id result, NSError *error) {
if (error)
{
completionHandler(nil,error);
}else{
completionHandler(result,nil);
}
}];
}
平台的判断 如果未安装不显示
//弹出分享平台 默认实现 以下几种
- (void)shareConfig
{
platFormArray =[NSMutableArray array];
if ( [QQApiInterface isQQInstalled] && [QQApiInterface isQQSupportApi]) {
//安装QQ
[platFormArray addObject:@(UMSocialPlatformType_QQ)];
[platFormArray addObject:@(UMSocialPlatformType_Qzone)];
}
if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
//安装微信
[platFormArray addObject:@(UMSocialPlatformType_WechatSession)];
[platFormArray addObject:@(UMSocialPlatformType_WechatTimeLine)];
}
[platFormArray addObject:@(UMSocialPlatformType_Sina)];
}
项目的使用方法
//平台选择 图文链接分享
[[NewShareManage shareManage]umSocial_ShareWithControll:self withPlatShareType:UMS_SHARE_TYPE_ALL withTitle:@"标题" withCotent:nil withImage:@"https://3658mall.oss-cn-qingdao.aliyuncs.com/images/20170925/goods/730_G_fd5de9df2be1cee32c5a18fd985523f3.png" withWebUrl:nil withCompletionHandler:^(id result, NSError *error) {
}];
//直接分享到某一平台 图片分享
[[NewShareManage shareManage]umSocial_ShareWithControll:self withOutPlatShareType:UMS_SHARE_TYPE_ALL withPostType:UMSocialPlatformType_QQ withTitle:@"111" withCotent:nil withImage:nil withWebUrl:nil withCompletionHandler:^(id result, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([result isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = result;
//分享结果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的数据
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",result);
}
}
}];
后记
NewShareManage的地址在此,有需要的可以拿来玩玩。