友盟分享方法详解(不包含集成和配置)

如果用到微博分享时,调用微博app失败,或者网页分享报400 bad request 错误,在配置appid等正确的情况下,造成这样的原因是:微博分享内容的字数限制在140以内


友盟分享环境集成与配置参照官方文档:点击打开链接

具体代码如下:

- (void)share:(NSInteger )index{
    NSString *shareType = @"";
    NSString *content = @"分享内容";
    NSString *title = @"分享标题";
    NSString *imageUrl = self.detailModel.image;
    NSString *contentUrl = [NSString stringWithFormat:@"http://wx.51jinmai.com/post/detail/%@", self.detailModel.postDetailId];
    id imageSource;
    if(imageUrl && imageUrl.length){
        imageSource = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageUrl]];
    }else{
        imageSource = kImageWithName(@"icon-60@3x");
    }
    switch (index){
        case 0:
            //微信好友
            shareType = UMShareToWechatTimeline;
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = contentUrl;
            [UMSocialData defaultData].extConfig.wechatTimelineData.title = title;
            [UMSocialData defaultData].extConfig.wechatTimelineData.wxMessageType = UMSocialWXMessageTypeWeb;
            break;
        case 1:
            //朋友圈
            shareType = UMShareToWechatSession;
            [UMSocialData defaultData].extConfig.wechatSessionData.url = contentUrl;
            [UMSocialData defaultData].extConfig.wechatSessionData.title = title;
            [UMSocialData defaultData].extConfig.wechatSessionData.wxMessageType = UMSocialWXMessageTypeWeb;
            break;
        case 2:
            //新浪微博
            shareType = UMShareToSina;
            //微博中提示的网页链接,需要手动加入content中
            content = [NSString stringWithFormat:@"%@ %@",title, contentUrl];
            break;
        case 3:
            shareType = UMShareToQQ;
            [UMSocialData defaultData].extConfig.qqData.url = contentUrl;
            [UMSocialData defaultData].extConfig.qqData.title = title;
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            break;
        case 4:
            shareType = UMShareToSms;
            //短信发送
            content = [NSString stringWithFormat:@"%@ %@",title, contentUrl];
            imageSource = nil;
            break;
        case 5:
            [self copyTextUrl];
            //复制链接
            return;
            break;
        default:
            break;
    }
    //跳转到相应的应用中进行分享
    [[UMSocialDataService defaultDataService] postSNSWithTypes:@[shareType] content:content image:imageSource location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess){
            NSLog(@"分享成功!");
        }
    }];
}


//复制链接

- (void)copyTextUrl{
    [MBProgressHUD showSuccess:@"复制成功" toView:nil];
    NSString *contentUrl = [NSString stringWithFormat:@"http://wx.51jinmai.com/post/detail/%@", self.detailModel.postDetailId];
    UIPasteboard *generalPasteBoard = [UIPasteboard generalPasteboard];
    [generalPasteBoard setString:contentUrl];
}


分享到朋友圈的时候,点开链接进入的是友盟网页,这是因为你设置的url不正确或者没有设置;

分享到微博,里面的链接是自己手动输入在content中,微博会自动识别http并将其转化为链接提示

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值