//
//? 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;
/**微信登錄授權(quán)---授權(quán)并獲取用戶(hù)信息(獲取uid、access token及用戶(hù)名等)*/
- (void)getAuthWithUserInfoFromWechat;
/**QQ - 授權(quán)并獲取用戶(hù)信息(獲取uid、access token及用戶(hù)名等)*/
- (void)getAuthWithUserInfoFromQQ;
/**新浪微博 授權(quán)并獲取用戶(hù)信息(獲取uid、access token及用戶(hù)名等)*/
- (void)getAuthWithUserInfoFromSina;
/**分享文本*/
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType;
/**分享圖片*/
- (void)shareImageToPlatformType:(UMSocialPlatformType)platformType;
/**圖文分享*/
- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType;
/**分享網(wǎng)頁(yè)*/
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType;
/**分享音樂(lè)*/
- (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];
? ? /* 設(shè)置微信的appKey和appSecret */
? ? /**
?? ? UMSocialPlatformType_WechatSession? ? ? = 1, //微信聊天
?? ? UMSocialPlatformType_WechatTimeLine? ? = 2,//微信朋友圈
?? ? UMSocialPlatformType_QQ? ? ? ? ? ? ? ? = 4,//QQ聊天頁(yè)面
?? ? UMSocialPlatformType_Qzone? ? ? ? ? ? ? = 5,//qq空間
?? ? */
? ? [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:App_WX_AppKey appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
? ? /*
?? ? * 移除相應(yīng)平臺(tái)的分享,如微信收藏
?? ? */
? ? //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
? ? /* 設(shè)置分享到QQ互聯(lián)的appID
?? ? * U-Share SDK為了兼容大部分平臺(tái)命名,統(tǒng)一用appKey和appSecret進(jìn)行參數(shù)設(shè)置,而QQ平臺(tái)僅需將appID作為U-Share的appKey參數(shù)傳進(jìn)即可。
?? ? */
? ? [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:APP_QQ_ID/*設(shè)置QQ平臺(tái)的appID*/? appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
? ? /* 設(shè)置新浪的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調(diào)用,是為了兼容國(guó)外平臺(tái)(例如:新版facebookSDK,VK等)的調(diào)用[如果用6.2的api調(diào)用會(huì)沒(méi)有回調(diào)],對(duì)國(guó)內(nèi)平臺(tái)沒(méi)有影響
? ? BOOLresult = [[UMSocialManagerdefaultManager]handleOpenURL:urlsourceApplication:sourceApplicationannotation:annotation];
? ? returnresult;
}
/**微信登錄授權(quán)---授權(quán)并獲取用戶(hù)信息(獲取uid、access token及用戶(hù)名等)*/
- (void)getAuthWithUserInfoFromWechat
{
? ? [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
? ? ? ? if(error) {
? ? ? ? }else{
? ? ? ? ? ? UMSocialUserInfoResponse*resp = result;
? ? ? ? ? ? // 授權(quán)信息
? ? ? ? ? ? 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);
? ? ? ? ? ? // 用戶(hù)信息
? ? ? ? ? ? NSLog(@"Wechat name: %@", resp.name);
? ? ? ? ? ? NSLog(@"Wechat iconurl: %@", resp.iconurl);
? ? ? ? ? ? NSLog(@"Wechat gender: %@", resp.unionGender);
? ? ? ? ? ? // 第三方平臺(tái)SDK源數(shù)據(jù)
? ? ? ? ? ? NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
? ? ? ? }
? ? }];
}
/**QQ - 授權(quán)并獲取用戶(hù)信息(獲取uid、access token及用戶(hù)名等)*/
- (void)getAuthWithUserInfoFromQQ
{
? ? [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
? ? ? ? if(error) {
? ? ? ? }else{
? ? ? ? ? ? UMSocialUserInfoResponse*resp = result;
? ? ? ? ? ? // 授權(quán)信息
? ? ? ? ? ? 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);
? ? ? ? ? ? // 用戶(hù)信息
? ? ? ? ? ? NSLog(@"QQ name: %@", resp.name);
? ? ? ? ? ? NSLog(@"QQ iconurl: %@", resp.iconurl);
? ? ? ? ? ? NSLog(@"QQ gender: %@", resp.unionGender);
? ? ? ? ? ? // 第三方平臺(tái)SDK源數(shù)據(jù)
? ? ? ? ? ? NSLog(@"QQ originalResponse: %@", resp.originalResponse);
? ? ? ? }
? ? }];
}
/**新浪微博 授權(quán)并獲取用戶(hù)信息(獲取uid、access token及用戶(hù)名等)*/
- (void)getAuthWithUserInfoFromSina
{
? ? [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
? ? ? ? if(error) {
? ? ? ? }else{
? ? ? ? ? ? UMSocialUserInfoResponse*resp = result;
? ? ? ? ? ? // 授權(quán)信息
? ? ? ? ? ? NSLog(@"Sina uid: %@", resp.uid);
? ? ? ? ? ? NSLog(@"Sina accessToken: %@", resp.accessToken);
? ? ? ? ? ? NSLog(@"Sina refreshToken: %@", resp.refreshToken);
? ? ? ? ? ? NSLog(@"Sina expiration: %@", resp.expiration);
? ? ? ? ? ? // 用戶(hù)信息
? ? ? ? ? ? NSLog(@"Sina name: %@", resp.name);
? ? ? ? ? ? NSLog(@"Sina iconurl: %@", resp.iconurl);
? ? ? ? ? ? NSLog(@"Sina gender: %@", resp.unionGender);
? ? ? ? ? ? // 第三方平臺(tái)SDK源數(shù)據(jù)
? ? ? ? ? ? NSLog(@"Sina originalResponse: %@", resp.originalResponse);
? ? ? ? }
? ? }];
}
/**
?分享請(qǐng)參考鏈接:https://developer.umeng.com/docs/66632/detail/66825#h3-u8C03u7528u5206u4EABu9762u677F
?有分享模版設(shè)置、自定義平臺(tái)等等。
?*/
/**分享文本*/
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? //設(shè)置文本
? ? messageObject.text = @"社會(huì)化組件UShare將各大社交平臺(tái)接入您的應(yīng)用,快速武裝App。";
? ? //調(diào)用分享接口
? ? [[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
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? //創(chuàng)建圖片內(nèi)容對(duì)象
? ? UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
? ? //如果有縮略圖,則設(shè)置縮略圖
? ? shareObject.thumbImage= [UIImageimageNamed:@"icon"];
? ? [shareObjectsetShareImage:@"https://mobile.umeng.com/images/pic/home/social/img-1.png"];
? ? //分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
? ? messageObject.shareObject= shareObject;
? ? //調(diào)用分享接口
? ? [[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
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? //設(shè)置文本
? ? messageObject.text = @"社會(huì)化組件UShare將各大社交平臺(tái)接入您的應(yīng)用,快速武裝App。";
? ? //創(chuàng)建圖片內(nèi)容對(duì)象
? ? UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
? ? //如果有縮略圖,則設(shè)置縮略圖
? ? shareObject.thumbImage= [UIImageimageNamed:@"icon"];
? ? [shareObjectsetShareImage:@"https://www.umeng.com/img/index/demo/1104.4b2f7dfe614bea70eea4c6071c72d7f5.jpg"];
? ? //分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
? ? messageObject.shareObject= shareObject;
? ? //調(diào)用分享接口
? ? [[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);
? ? ? ? }
? ? }];
}
/**分享網(wǎng)頁(yè)*/
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? //創(chuàng)建網(wǎng)頁(yè)內(nèi)容對(duì)象
? ? UMShareWebpageObject*shareObject = [UMShareWebpageObjectshareObjectWithTitle:@"分享標(biāo)題"descr:@"分享內(nèi)容描述"thumImage:[UIImageimageNamed:@"icon"]];
? ? //設(shè)置網(wǎng)頁(yè)地址
? ? shareObject.webpageUrl =@"http://mobile.umeng.com/social";
? ? //分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
? ? messageObject.shareObject= shareObject;
? ? //調(diào)用分享接口
? ? [[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);
? ? ? ? }
? ? }];
}
/**分享音樂(lè)*/
- (void)shareMusicToPlatformType:(UMSocialPlatformType)platformType
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? //創(chuàng)建音樂(lè)內(nèi)容對(duì)象
? ? UMShareMusicObject*shareObject = [UMShareMusicObjectshareObjectWithTitle:@"分享標(biāo)題"descr:@"分享內(nèi)容描述"thumImage:[UIImageimageNamed:@"icon"]];
? ? //設(shè)置音樂(lè)網(wǎng)頁(yè)播放地址
? ? shareObject.musicUrl = @"http://c.y.qq.com/v8/playsong.html?songid=108782194&source=yqq#wechat_redirect";
? ? //? ? ? ? ? ? shareObject.musicDataUrl = @"這里設(shè)置音樂(lè)數(shù)據(jù)流地址(如果有的話,而且也要看所分享的平臺(tái)支不支持)";
? ? //分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
? ? messageObject.shareObject= shareObject;
? ? //調(diào)用分享接口
? ? [[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
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? //創(chuàng)建視頻內(nèi)容對(duì)象
? ? UMShareVideoObject*shareObject = [UMShareVideoObjectshareObjectWithTitle:@"分享標(biāo)題"descr:@"分享內(nèi)容描述"thumImage:[UIImageimageNamed:@"icon"]];
? ? //設(shè)置視頻網(wǎng)頁(yè)播放地址
? ? shareObject.videoUrl = @"http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html";
? ? //? ? ? ? ? ? shareObject.videoStreamUrl = @"這里設(shè)置視頻數(shù)據(jù)流地址(如果有的話,而且也要看所分享的平臺(tái)支不支持)";
? ? //分享消息對(duì)象設(shè)置分享內(nèi)容對(duì)象
? ? messageObject.shareObject= shareObject;
? ? //調(diào)用分享接口
? ? [[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;
? ? //調(diào)用分享接口
? ? [[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;
? ? ? ? ? ? ? ? //分享結(jié)果消息
? ? ? ? ? ? ? ? NSLog(@"response message is %@",resp.message);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? NSLog(@"response data is %@",data);
? ? ? ? ? ? }
? ? ? ? }
? ? }];
}
/**分享微信小程序*/
- (void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType
{
? ? //創(chuàng)建分享消息對(duì)象
? ? UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
? ? UMShareMiniProgramObject*shareObject = [UMShareMiniProgramObjectshareObjectWithTitle:@"小程序標(biāo)題"descr:@"小程序內(nèi)容描述"thumImage:[UIImageimageNamed:@"icon"]];
? ? shareObject.webpageUrl=@"兼容微信低版本網(wǎng)頁(yè)地址";
? ? shareObject.userName = @"小程序username,如 gh_3ac2059ac66f";
? ? shareObject.path = @"小程序頁(yè)面路徑,如 pages/page10007/page10007";
? ? messageObject.shareObject= shareObject;
? ? shareObject.hdImageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]];
? ? shareObject.miniProgramType = UShareWXMiniProgramTypeRelease; // 可選體驗(yàn)版和開(kāi)發(fā)板
? ? //調(diào)用分享接口
? ? [[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;
? ? ? ? ? ? ? ? //分享結(jié)果消息
? ? ? ? ? ? ? ? UMSocialLogInfo(@"response message is %@",resp.message);
? ? ? ? ? ? ? ? //第三方原始返回的數(shù)據(jù)
? ? ? ? ? ? ? ? UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? UMSocialLogInfo(@"response data is %@",data);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //? ? ? ? [self alertWithError:error];
? ? }];
}
@end