专栏名称: 3033
iOS开发
目录
相关文章推荐
潇湘晨报  ·  官方通报:情况属实!已暂停工作 ·  18 小时前  
湖南建设投资集团有限责任公司  ·  晒项目 展业绩 ... ·  20 小时前  
51好读  ›  专栏  ›  3033

2017友盟最新封装与CocoaPods升级

3033  · 掘金  ·  · 2017-12-14 00:35

正文

####起因 因为要更新老项目中的一个远古时期的友盟分享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 set






请到「今天看啥」查看全文