//配置啥的就不多说了,去看官方文档
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//在didFinish这个方法中注册UM,多的就不写了
//友盟,打开日志
[[UMSocialManager defaultManager] openLog:YES];
//设置友盟appkey
[[UMSocialManager defaultManager] setUmSocialAppkey:UM_APPKey];
[self configUSharePlatforms];
}
#pragma mark -- 友盟分享设置
- (void)configUSharePlatforms{
/* 设置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxd5af95220867b52e" appSecret:@"fc206169c6ffc4b6dc392f81133eca9e" redirectURL:nil];
//移除微信收藏,这个是移除不需要的面板,因为没注意看文档这个没找到,还是浪费了一些时间的,所以说文档很重要,一定要细心看哦小伙伴们
[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_Sina)]];
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106169079" appSecret:@"7CnfJR5J2RbukmnF" redirectURL:nil];
//在这里我就用到了微信和QQ的其他的同理只要配置好在面板上添加,增加他们的appKey和appSecret就行了。
}
// 在这里我封装了一个类来方便调用分享就不用写重复的代码了,我的这个类名叫做 UMShareObjct
//这个是分享按钮点击事件,因为分享需要一个控制器所以我顺带也把控制器传过去了
- (void)shareButtonAction:(UIButton *)btn {
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
// 根据获取的platformType确定所选平台进行下一步操作
NSLog(@"platformType == %ld",(long)platformType);
[UMShareObjct shareWebPageToPlatformType:platformType ViewCoViewController:self URLDic:self.dataDic Type:1];
}];
}
//UMShareObjct.h里面的代码
#import <Foundation/Foundation.h>
#import <UMSocialCore/UMSocialCore.h>
@interface UMShareObjct : NSObject
+ (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType ViewCoViewController:(UIViewController *)viewController URLDic:(NSDictionary *)dic Type:(NSInteger)type;
+ (UIImage *) getImageFromURL:(NSString *)fileURL;
@end
//UMShareObjct.m里面的代码
//
// UMShareObjct.m
// tea
//
// Created by nzrj on 2017/6/16.
// Copyright © 2017年 jiangyunrong. All rights reserved.
//
#import "UMShareObjct.h"
#import "AlertCustomView.h"
@implementation UMShareObjct
+ (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType ViewCoViewController:(UIViewController *)viewController URLDic:(NSDictionary *)dic Type:(NSInteger)type{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
NSString *thumbURL;
if(type == 1){
thumbURL = [NSString stringWithFormat:@"%@%@",ImageRequest,dic[@"img"]];
NSLog(@"tehunnn == %@",thumbURL);
}else if (type == 2){
thumbURL = [NSString stringWithFormat:@"%@%@",ImageRequest,dic[@"titlepic"]];
NSLog(@"tehunnn == %@",thumbURL);
}
//在这里调用,是因为我发现我的图片怎么都不会出来,加了这个就出来了
UIImage *img = [UMShareObjct getImageFromURL:thumbURL];
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:dic[@"title"] descr:dic[@"content"] thumImage:img];
//设置网页地址
shareObject.webpageUrl = dic[@"shareurl"];
//分享消息对象摄者分享内容对象
messageObject.shareObject = shareObject;
//调用分享借口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:viewController completion:^(id result, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([result isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = result;
//分享结果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的数据
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",result);
}
}
// [[MyAlertView sharedInstance]showFrom:[NSString stringWithFormat:@"%@",error]];
}];
}
+ (UIImage *) getImageFromURL:(NSString *)fileURL{
NSLog(@"执行图片下载函数");
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
@end
iOS 友盟分享小技巧
最新推荐文章于 2024-08-26 21:28:20 发布