关于分享那些事
在iOS 或者安卓平台上实现社会化分享功能,一般就是通过第三方的SDK进行分享服务,如友盟分享,ShareSDK分享
一种是把有梦分享的SDK集成到工程里面,直接调用SDK里面API的方法进行分享,按照友盟分享的开发文档直接进行集成,注意appkey 还有分享到第三方,需要去相应的平台里面申请相应的appkey和appsecret,然后在配置文件里面配置相应的回调地址和网页,直接进行分享,这种方法看集成文档就能完成,不在做解释,在这里主要讲一下第二种方法。
另外一种方法就是在H5网页分享,这个适用于大型的app,工程里面需要集成第三方的H5页面,把第三方的H5页面直接嵌套在控制器里面,遵循里面的代理方法实现加载第三方合作单位及成员机构里面的H5页面。通过写入一段js代码段,调起应用里面的方法及相应的操作。
安卓注入的js代码段
<script type="text/javascript">
function fx(){
$.ajax({
url: "${ctx}/app/homepage/fx.do",
dataType:"json",
success: function(data){
//这里是调用应用里面的方法--安卓
window.share.postMessage({title:'测试分享的标题',content:'测试分享的内容',url:'http://www.cnblog.com'});
}
});
}
</script>
iOS 注入的代码段
<script type="text/javascript">
function fx(){
$.ajax({
url: "${ctx}/app/homepage/fx.do",
dataType:"json",
success: function(data){
//这里是iOS 调用的方法名
window.webkit.messageHandlers.share.postMessage({title:'测试分享的标题',content:'测试分享的内容',url:'http://www.cnblog.com'});
}
});
}
</script>
//注意标明颜色为前台与后台约定好的方法名,必须保持一致,否则不能进行分享操作
这里已友盟分享,iOS端为例
//js调用OC代码段实现分享功能
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
NSLog(@"%@",message);
NSLog(@"body:%@",message.body);
if ([message.name isEqualToString:@"share"]) {
NSLog(@"调用成功");
[self shareing:message.body];
}
}
#pragma mark ---打开分享面板
- (void)shareing:(NSDictionary *)tempDic{
if (![tempDic isKindOfClass:[NSDictionary class]]) {
return;
}
NSString *title = [tempDic objectForKey:@"title"];
NSString *content = [tempDic objectForKey:@"content"];
NSString *url = [tempDic objectForKey:@"url"];
//显示分享面板
// UMSocialUIManager
[UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;
[UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_None;
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建网页内容对象
UIImage* thumbURL = [UIImage imageNamed:@"11.png"];
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:thumbURL];
//设置网页地址
shareObject.webpageUrl = url;
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享结果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的数据
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self alertWithError:error];
}];
}];
}
- (void)alertWithError:(NSError *)error
{
NSString *result = nil;
if (!error) {
result = [NSString stringWithFormat:@"分享成功"];
}
else{
NSMutableString *str = [NSMutableString string];
if (error.userInfo) {
for (NSString *key in error.userInfo) {
[str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
}
}
if (error) {
result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
}
else{
result = [NSString stringWithFormat:@"分享失败"];
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享"
message:result
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", @"确定")
otherButtonTitles:nil];
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[userContentController removeScriptMessageHandlerForName:@"share"]; //关闭web页时会释放内存
}
//还需要在工程里面配置一些回调,来展示分享成功或分享失败操作 返回分享成功或分享失败标题
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
}
return result;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
}
return result;
}
//在这里就大功告成啦,时间匆忙,如有不对的地方欢迎指正
你的赞赏是对我最大的鼓励,谢谢