最近做分享功能,用了友盟分享,遇到了很多坑(以前都没有遇到过),所以就分享了一下
如果按照上面的做,应该不会有什么问题了,当然,这只是我的个人看法
今天居然又遇到了个深坑
2.分享视频(横屏时,用的是自定义的分享视图)
正确代码( 注:必须重新设置urlResource,否则分享的类型会乱,虽然分享的是视频,但是链接可能是图片,或者是一个网页链接)
之前做的分享,大都是分享的都是本地的图片,加上标题,内容
现在这个项目要求分享从服务器获得的图片,
下面是友盟分享的代码,利用默认的分享界面
[UMSocialSnsService presentSnsIconSheetView:self
appKey:UM_APPKEY
shareText:[NSString stringWithFormat:@"%@ %@",shareText,shareURL]
shareImage:image
shareToSnsNames:@[UMShareToQzone,UMShareToQQ,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina]
delegate:self];
第一个参数:在哪个Controller上添加分享界面
第二个参数:友盟的APPKey
第三个参数:分享的文本(如果要分享的新浪微博的话,需要把分享的链接加载分享的内容后面,上面的代码就是)
第四个参数:你要分享的图片(UIImage类型或者NSData类型,我都用UIImage类型)
第五个参数:你所要分享的平台(顺序自定,默认的分享界面会根据该数据的顺序排序)
第六个参数:代理,self
分享服务器上的数据:链接,图片,标题,内容
1.把图片转换为NSData类型,然后再把NSData类型转换为UIImage类型
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareImage]];
UIImage * image = [UIImage imageWithData:data];
2.最好记得设置一个默认图片
if (!image) {
image = [UIImage imageNamed:@"AppIcon"];
}
3.设置标题
[UMSocialData defaultData].extConfig.title = shareTitle;
4.为了分享不会失败,设置默认链接,
if ([shareURL isEqualToString:@""] || shareURL.length == 0) {
shareURL = DEFAULT_URL;
}
5.设置分享链接(大坑),
如果不存在,分享QQ(分享失败,打不开QQ),分享到微信(信息显示发送失败),所以最好要有第4步
[UMSocialData defaultData].extConfig.qqData.url = shareURL;
[UMSocialData defaultData].extConfig.wechatSessionData.url = shareURL;
[UMSocialData defaultData].extConfig.wechatTimelineData.url = shareURL;
[UMSocialData defaultData].extConfig.qzoneData.url = shareURL;
6.不要轻易设置,设置下面的东西,发现分享只有一张图片,其他的类型也是
[[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:@""];
下面是完整代码
-(void)share{
//分享
NSDictionary * shareDic = _currentInfo.shareInfo;
HRShareInfoModel * model =[HRShareInfoModel objectWithKeyValues:shareDic];
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.shareImage]];
UIImage * image = [UIImage imageWithData:data];
if (!image) {
image = [UIImage imageNamed:@"AppIcon"];
}
//标题
[UMSocialData defaultData].extConfig.title = model.shareTitle;
if ([model.shareURL isEqualToString:@""] || model.shareURL.length == 0) {
model.shareURL = DEFAULT_URL;
}
//链接
[UMSocialData defaultData].extConfig.qqData.url = model.shareURL;
[UMSocialData defaultData].extConfig.wechatSessionData.url = model.shareURL;
[UMSocialData defaultData].extConfig.wechatTimelineData.url = model.shareURL;
[UMSocialData defaultData].extConfig.qzoneData.url = model.shareURL;
[UMSocialSnsService presentSnsIconSheetView:self
appKey:UM_APPKEY
shareText:[NSString stringWithFormat:@"%@ %@",model.shareText,model.shareURL]
shareImage:image
shareToSnsNames:@[UMShareToQzone,UMShareToQQ,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina]
delegate:self];
}
今天居然又遇到了个深坑
在项目中,需要分享视频,分享图片,分享图链接
1.分享图文链接 ,下面只是Q分享的代码,用的是友盟默认的分享视图
<span style="font-size:14px;">UMSocialUrlResource * urlResource = [[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:model.shareImage];</span><pre name="code" class="objc">[UMSocialData defaultData].extConfig.qqData.title = model.shareTitle;
[UMSocialData defaultData].extConfig.qqData.shareText = model.shareText;
[UMSocialData defaultData].extConfig.qqData.url = model.shareURL;
[UMSocialData defaultData].extConfig.qqData.urlResource= urlResource;<pre name="code" class="objc">[UMSocialSnsService presentSnsIconSheetView:self
appKey:UM_APPKEY
shareText:nil
shareImage:nil
shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToSina]
delegate:self];
2.分享视频(横屏时,用的是自定义的分享视图)
错误代码
UMSocialUrlResource * resourec = [UMSocialData defaultData].urlResource;
[resourec setResourceType:UMSocialUrlResourceTypeVideo url:model.videoUrl];
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[type] content:model.title image:nil location:nil urlResource:resourec presentedController:nil completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功!");
}
}];
正确代码( 注:必须重新设置urlResource,否则分享的类型会乱,虽然分享的是视频,但是链接可能是图片,或者是一个网页链接)
UMSocialUrlResource * resourec = [UMSocialData defaultData].urlResource;
[resourec setResourceType:UMSocialUrlResourceTypeVideo url:model.videoUrl];
[UMSocialData defaultData].extConfig.wechatSessionData.urlResource = resourec;
[UMSocialData defaultData].extConfig.sinaData.urlResource = resourec;
[UMSocialData defaultData].extConfig.sinaData.shareText = [NSString stringWithFormat:@"%@%@",model.title,model.videoUrl];
[UMSocialData defaultData].extConfig.wechatTimelineData.urlResource = resourec;
[UMSocialData defaultData].extConfig.qqData.urlResource = resourec;
[UMSocialData defaultData].extConfig.qzoneData.urlResource = resourec;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[type] content:model.title image:nil location:nil urlResource:resourec presentedController:nil completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功!");
}
}];
3.分享图片
错误代码 (注:必须重新设置urlResource,否则分享的类型会乱,分享的结果可能是个图文链接,也可能是个视频)
UMSocialUrlResource * resourec = [UMSocialData defaultData].urlResource;
[resourec setResourceType:UMSocialUrlResourceTypeImage url:nil];
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[type] content:nil image:image location:nil urlResource:nil presentedController:nil completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功!");
}
}];
正确代码
UMSocialUrlResource * resourec = [UMSocialData defaultData].urlResource;
[resourec setResourceType:UMSocialUrlResourceTypeImage url:nil];
[UMSocialData defaultData].extConfig.wechatSessionData.urlResource = resourec;
[UMSocialData defaultData].extConfig.sinaData.urlResource = resourec;
[UMSocialData defaultData].extConfig.sinaData.shareText = @"浦东新闻台";
[UMSocialData defaultData].extConfig.wechatTimelineData.urlResource = resourec;
[UMSocialData defaultData].extConfig.qqData.urlResource = resourec;
[UMSocialData defaultData].extConfig.qzoneData.urlResource = resourec;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[type] content:nil image:image location:nil urlResource:nil presentedController:nil completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功!");
}
}];
注:如果项目中同时出现多种分享类型的话,分享的时候都需要去设置urlSource