友盟分享(精简版)

作者: Renjiee | 来源:发表于2017-06-27 11:57 被阅读107次

做过几次分享,这次我也把我的经验总结分享给大家吧,只做的QQ和微信的分享

1.登录友盟官网去添加一个应用

添加应用
2.填写好应用相应信息 填写信息

3.保存得到的AppKey,后面在工程中会使用到

这里附上官方分享文档链接友盟分享

首先在工程中导入友盟分享SDK

友盟分享SDK

我是直接在官网下载所需要的精简版,然后直接导入,上面链接有pods导入教程

添加以下系统依赖库👇

添加系统依赖库

代码集成

打开工程AppDelegate.h文件夹导入头文件
#import <UMSocialCore/UMSocialCore.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:YES];
    /* 设置友盟appkey */
    [[UMSocialManager defaultManager] setUmSocialAppkey:USHARE_DEMO_APPKEY];
    
    [self configUSharePlatforms];
    
    return YES;
}
- (void)configUSharePlatforms
{
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WXAPPKEY appSecret:WXAppSecret redirectURL:@"http://mobile.umeng.com/social"];
    
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAPPKEY/*设置QQ平台的appID*/  appSecret:QQAppSecret redirectURL:@"http://mobile.umeng.com/social"];
    
    
}

USHARE_DEMO_APPKEY友盟的APPKEY宏

界面展示和分享调用

自定义一个UICollectionView

#pragma mark -------------------- UICollectionViewDataSource --------------------

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.shareIconAry.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    self.shareCell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    //    [cell sizeToFit];
    
    self.shareCell.icon.image = [UIImage imageNamed:self.shareIconAry[indexPath.row]];
    
    return self.shareCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;

    switch (indexPath.row) {
        case 0:
            [weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];

            break;
        case 1:
            [weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
            
            break;
        case 2:
            [weakSelf shareWebPageToPlatformType:UMSocialPlatformType_QQ];
            
            break;
        case 3:
            [weakSelf shareWebPageToPlatformType:UMSocialPlatformType_Qzone];
            
            break;
            
        default:
            break;
    }
}

分享回调

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
//    NSString* thumbURL =  @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    
    NSString * link = @"share_download";
    NSString * thumbURL = [[NSString alloc]initWithFormat:@"www.baidu.com"];
    // 将链接转译为UTF8因为 codeStr中有中文会导致QQ分享链接出错
    NSString* encodedString = [thumbURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"百度一下,你就知道" descr:nil thumImage:encodedString];
    //设置网页地址
    shareObject.webpageUrl = encodedString;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
            [LCProgressHUD showFailure:@"分享失败"];

        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                [LCProgressHUD showSuccess:@"分享成功"];
                
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
//        [self alertWithError:error];
    }];
}

效果图

效果图

代码Demo我已经上传到了GitHub 下载地址
还请大家多多提意见 喜欢的给个Stare

我是Renjiee 我要做最骚的程序猿👨‍💻‍

相关文章

网友评论

    本文标题:友盟分享(精简版)

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