//
// UIViewController+Share.h
//
//
// Created by wzk on 2019/1/11.
// Copyright © 2019 wzk. All rights reserved.
//
/**
配置和参考代码:https://developer.umeng.com/docs/66632/detail/66898
*/
#import
NS_ASSUME_NONNULL_BEGIN
@interfaceUIViewController (Share)
- (void)configUSharePlatforms;
- (BOOL)application:(UIApplication*)application url:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation;
/**微信登录授权---授权并获取用户信息(获取uid、access token及用户名等)*/
- (void)getAuthWithUserInfoFromWechat;
/**QQ - 授权并获取用户信息(获取uid、access token及用户名等)*/
- (void)getAuthWithUserInfoFromQQ;
/**新浪微博 授权并获取用户信息(获取uid、access token及用户名等)*/
- (void)getAuthWithUserInfoFromSina;
/**分享文本*/
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType;
/**分享图片*/
- (void)shareImageToPlatformType:(UMSocialPlatformType)platformType;
/**图文分享*/
- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType;
/**分享网页*/
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType;
/**分享音乐*/
- (void)shareMusicToPlatformType:(UMSocialPlatformType)platformType;
/**分享视频*/
- (void)shareVedioToPlatformType:(UMSocialPlatformType)platformType;
/**分享微信表情*/
- (void)shareEmoticonToPlatformType:(UMSocialPlatformType)platformType;
/**分享微信小程序*/
- (void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType;
@end
NS_ASSUME_NONNULL_END
//
// UIViewController+Share.m
// IronFish
//
// Created by wzk on 2019/1/11.
// Copyright © 2019 wzk. All rights reserved.
//
#import "UIViewController+Share.h"
@implementationUIViewController (Share)
- (void)configUSharePlatforms
{
/**友盟*/
// [UMSocialData setAppKey:@""];
[UMConfigure initWithAppkey:App_UMeng_AppKey channel:nil];
/* 设置微信的appKey和appSecret */
/**
UMSocialPlatformType_WechatSession = 1, //微信聊天
UMSocialPlatformType_WechatTimeLine = 2,//微信朋友圈
UMSocialPlatformType_QQ = 4,//QQ聊天页面
UMSocialPlatformType_Qzone = 5,//qq空间
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:App_WX_AppKey appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
/*
* 移除相应平台的分享,如微信收藏
*/
//[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
/* 设置分享到QQ互联的appID
* U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:APP_QQ_ID/*设置QQ平台的appID*/ appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
/* 设置新浪的appKey和appSecret */
// [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"" appSecret:@"" redirectURL:@"https://"];
[[UMSocialManager defaultManager] openLog:YES];
}
- (BOOL)application:(UIApplication*)application url:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOLresult = [[UMSocialManagerdefaultManager]handleOpenURL:urlsourceApplication:sourceApplicationannotation:annotation];
returnresult;
}
/**微信登录授权---授权并获取用户信息(获取uid、access token及用户名等)*/
- (void)getAuthWithUserInfoFromWechat
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
if(error) {
}else{
UMSocialUserInfoResponse*resp = result;
// 授权信息
NSLog(@"Wechat uid: %@", resp.uid);
NSLog(@"Wechat openid: %@", resp.openid);
NSLog(@"Wechat unionid: %@", resp.unionId);
NSLog(@"Wechat accessToken: %@", resp.accessToken);
NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
NSLog(@"Wechat expiration: %@", resp.expiration);
// 用户信息
NSLog(@"Wechat name: %@", resp.name);
NSLog(@"Wechat iconurl: %@", resp.iconurl);
NSLog(@"Wechat gender: %@", resp.unionGender);
// 第三方平台SDK源数据
NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
}
}];
}
/**QQ - 授权并获取用户信息(获取uid、access token及用户名等)*/
- (void)getAuthWithUserInfoFromQQ
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
if(error) {
}else{
UMSocialUserInfoResponse*resp = result;
// 授权信息
NSLog(@"QQ uid: %@", resp.uid);
NSLog(@"QQ openid: %@", resp.openid);
NSLog(@"QQ unionid: %@", resp.unionId);
NSLog(@"QQ accessToken: %@", resp.accessToken);
NSLog(@"QQ expiration: %@", resp.expiration);
// 用户信息
NSLog(@"QQ name: %@", resp.name);
NSLog(@"QQ iconurl: %@", resp.iconurl);
NSLog(@"QQ gender: %@", resp.unionGender);
// 第三方平台SDK源数据
NSLog(@"QQ originalResponse: %@", resp.originalResponse);
}
}];
}
/**新浪微博 授权并获取用户信息(获取uid、access token及用户名等)*/
- (void)getAuthWithUserInfoFromSina
{
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
if(error) {
}else{
UMSocialUserInfoResponse*resp = result;
// 授权信息
NSLog(@"Sina uid: %@", resp.uid);
NSLog(@"Sina accessToken: %@", resp.accessToken);
NSLog(@"Sina refreshToken: %@", resp.refreshToken);
NSLog(@"Sina expiration: %@", resp.expiration);
// 用户信息
NSLog(@"Sina name: %@", resp.name);
NSLog(@"Sina iconurl: %@", resp.iconurl);
NSLog(@"Sina gender: %@", resp.unionGender);
// 第三方平台SDK源数据
NSLog(@"Sina originalResponse: %@", resp.originalResponse);
}
}];
}
/**
分享请参考链接:https://developer.umeng.com/docs/66632/detail/66825#h3-u8C03u7528u5206u4EABu9762u677F
有分享模版设置、自定义平台等等。
*/
/**分享文本*/
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//设置文本
messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用,快速武装App。";
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if(error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
/**分享图片*/
- (void)shareImageToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建图片内容对象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有缩略图,则设置缩略图
shareObject.thumbImage= [UIImageimageNamed:@"icon"];
[shareObjectsetShareImage:@"https://mobile.umeng.com/images/pic/home/social/img-1.png"];
//分享消息对象设置分享内容对象
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{
NSLog(@"response data is %@",data);
}
}];
}
/**图文分享*/
- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//设置文本
messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用,快速武装App。";
//创建图片内容对象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有缩略图,则设置缩略图
shareObject.thumbImage= [UIImageimageNamed:@"icon"];
[shareObjectsetShareImage:@"https://www.umeng.com/img/index/demo/1104.4b2f7dfe614bea70eea4c6071c72d7f5.jpg"];
//分享消息对象设置分享内容对象
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{
NSLog(@"response data is %@",data);
}
}];
}
/**分享网页*/
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建网页内容对象
UMShareWebpageObject*shareObject = [UMShareWebpageObjectshareObjectWithTitle:@"分享标题"descr:@"分享内容描述"thumImage:[UIImageimageNamed:@"icon"]];
//设置网页地址
shareObject.webpageUrl =@"http://mobile.umeng.com/social";
//分享消息对象设置分享内容对象
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{
NSLog(@"response data is %@",data);
}
}];
}
/**分享音乐*/
- (void)shareMusicToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建音乐内容对象
UMShareMusicObject*shareObject = [UMShareMusicObjectshareObjectWithTitle:@"分享标题"descr:@"分享内容描述"thumImage:[UIImageimageNamed:@"icon"]];
//设置音乐网页播放地址
shareObject.musicUrl = @"http://c.y.qq.com/v8/playsong.html?songid=108782194&source=yqq#wechat_redirect";
// shareObject.musicDataUrl = @"这里设置音乐数据流地址(如果有的话,而且也要看所分享的平台支不支持)";
//分享消息对象设置分享内容对象
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{
NSLog(@"response data is %@",data);
}
}];
}
/**分享视频*/
- (void)shareVedioToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建视频内容对象
UMShareVideoObject*shareObject = [UMShareVideoObjectshareObjectWithTitle:@"分享标题"descr:@"分享内容描述"thumImage:[UIImageimageNamed:@"icon"]];
//设置视频网页播放地址
shareObject.videoUrl = @"http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html";
// shareObject.videoStreamUrl = @"这里设置视频数据流地址(如果有的话,而且也要看所分享的平台支不支持)";
//分享消息对象设置分享内容对象
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{
NSLog(@"response data is %@",data);
}
}];
}
/**分享微信表情*/
- (void)shareEmoticonToPlatformType:(UMSocialPlatformType)platformType
{
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
UMShareEmotionObject *shareObject = [UMShareEmotionObject shareObjectWithTitle:@"" descr:@"" thumImage:nil];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"gifFile"
ofType:@"gif"];
NSData*emoticonData = [NSDatadataWithContentsOfFile:filePath];
shareObject.emotionData= emoticonData;
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([dataisKindOfClass:[UMSocialShareResponseclass]]) {
UMSocialShareResponse*resp = data;
//分享结果消息
NSLog(@"response message is %@",resp.message);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
/**分享微信小程序*/
- (void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
UMShareMiniProgramObject*shareObject = [UMShareMiniProgramObjectshareObjectWithTitle:@"小程序标题"descr:@"小程序内容描述"thumImage:[UIImageimageNamed:@"icon"]];
shareObject.webpageUrl=@"兼容微信低版本网页地址";
shareObject.userName = @"小程序username,如 gh_3ac2059ac66f";
shareObject.path = @"小程序页面路径,如 pages/page10007/page10007";
messageObject.shareObject= shareObject;
shareObject.hdImageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]];
shareObject.miniProgramType = UShareWXMiniProgramTypeRelease; // 可选体验版和开发板
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if(error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if([dataisKindOfClass:[UMSocialShareResponseclass]]) {
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];
}];
}
@end