因之前都写在了 AppDelegate 类中,看起来过于臃肿,此次基于友盟分享重新进行了一次优化,此次分享内容基于已经成功集成 SDK 后 code 层级部分.
注:此次分享基于 SDK 6.9.3,若版本相差较大,仅供参考.
友盟分享官方文档: https://developer.umeng.com/docs/66632/detail/66825

Github: https://github.com/survivorsfyh/YHSampleCode/tree/master/UMShare

首先,为分享单独创建了一个类,为了可以更加清晰的划分其内容部分.

注:创建该子类后,切记将其头文件引入到 AppDelegate 类中.

#import "AppDelegate.h"
// Share
#import "AppDelegate+UMShare.h"

其次,校验项目工程中是否配置正确相关参数.

再其次,便是具体 code 相关内容,将申请的相关 key 预先设置成宏准备好,方便使用和变更时进行调用和更改.
一.声明类中(.h)
1.将初始化的相关接口配置优先声明准备好,并在 AppDelegate 类中调用.
2.将调用分享的具体执行方法接口预留好,在需要调用的地方调用该方法.
步骤一:

#import "AppDelegate.h"
#import <UMShare/UMShare.h> @interface AppDelegate (UMShare) /**
UMShare 注册
@param launchOptions 应用程序
*/
- (void)registerUMShare:(NSDictionary *)launchOptions; /**
调用友盟相关方法 @param controller 当前视图控件
*/
- (void)getUMShareRelevantMethodsWithCurrentViewController:(UIViewController *)controller; /**
调用友盟相关方法(带参) @param controller 当前视图控件
@param data 参数(shareType:分享类型(默认:base,若需要自定义则将 base 变更为其它) & descr:备注说明文字 & webpageUrl:跳转链接地址)
*/
- (void)getUMShareRelevantMethodsWithCurrentViewController:(UIViewController *)controller AndParameter:(NSMutableDictionary *)data; @end  

步骤二:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. // Share
[self registerUMShare:launchOptions];
// Root ViewController
[self setRootViewController]; // CoreData
[[CoreDataManager sharedCoreDataManager] managedObjectContext]; return YES;
}

二.实现类中(.m)
1.引入所需的头文件
2.实现声明类中接口
3.实现具体方法和代理事件

#import "AppDelegate+UMShare.h"
#import <UMShare/UMShare.h>
#import <UMCommon/UMCommon.h>
#import <UMCommonLog/UMCommonLogHeaders.h>
#import <UShareUI/UShareUI.h> @implementation AppDelegate (UMShare) /**
UMShare 注册 @param launchOptions 应用程序
*/
- (void)registerUMShare:(NSDictionary *)launchOptions {
// UMConfigure 通用设置,请参考SDKs集成做统一初始化。
// 以下仅列出U-Share初始化部分
[self configUSharePlatforms];
[self confitUShareSettings]; BOOL isSetLog;
#ifdef DEBUG
isSetLog = 0;
#else
isSetLog = 1;
#endif // Log
[UMCommonLogManager setUpUMCommonLogManager];
[UMConfigure setLogEnabled:isSetLog];
[UMConfigure initWithAppkey:UMSHARE_APPKEY channel:@"App Store"];
} /**
共享平台配置
*/
- (void)configUSharePlatforms {
/*
设置微信的 appKey 和 appSecret AppID: xxxxxx
AppSecret: xxxxxx
申请相关参数的 URL:https://open.weixin.qq.com/
腾讯相关 URL:https://open.tencent.com
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession
appKey:@"您项目申请的 key 值"
appSecret:@"您项目申请的 Secret 值"
redirectURL:@"您的官网门户链接即可"]; /*
设置分享到 QQ 互联的 appID
U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ
appKey:@"您项目申请的 key 值" /*设置QQ平台的appID*/
appSecret:@"您项目申请的 Secret 值" /*QQ平台为appKey*/
redirectURL:@"您的官网门户链接即可"];// aC5dY6D15Uz5wCv9 /*
* 移除相应平台的分享,如微信收藏
*/
// [[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
} /**
共享平台设置
*/
- (void)confitUShareSettings {
[UMSocialGlobal shareInstance].isClearCacheWhenGetUserInfo = YES;
/*
* 打开图片水印
*/
// [UMSocialGlobal shareInstance].isUsingWaterMark = YES; /*
* 关闭强制验证https,可允许http图片分享,但需要在info.plist设置安全域名
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
*/
// [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
} /**
调用友盟相关方法
@param controller 当前视图控件
*/
- (void)getUMShareRelevantMethodsWithCurrentViewController:(UIViewController *)controller {
// 设置预定义平台(即:需要分享至哪些平台就将其枚举值中的参数添加进数组中)
NSArray *sharePlatforms = @[@(UMSocialPlatformType_WechatSession),
@(UMSocialPlatformType_WechatTimeLine),
@(UMSocialPlatformType_QQ)];
[UMSocialUIManager setPreDefinePlatforms:sharePlatforms]; kWeakSelf(self);
// 显示分享面板
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
[weakself shareWebPageToPlatformType:platformType AndCurrentViewController:controller]; // [weakself checkUserInfoForPlatform:platformType AndCurrentViewController:controller];
}];
} /**
校验用户信息平台
@param platformType 平台类型
*/
- (void)checkUserInfoForPlatform:(UMSocialPlatformType)platformType AndCurrentViewController:(UIViewController *)controller {
kWeakSelf(self);
[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:nil completion:^(id result, NSError *error) {
// 当前设备 App 安装检测 [kApplication openURL:[NSURL URLWithString:@"weixin://"]] && [kApplication openURL:[NSURL URLWithString:@"mqq://"]]
if ([kApplication openURL:[NSURL URLWithString:@"mqq://"]] && [kApplication openURL:[NSURL URLWithString:@"weixin://"]]) {// WeChat @"weixin://"
[weakself shareWebPageToPlatformType:platformType AndCurrentViewController:controller];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"当前设备未安装该程序"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[alert show];
return;
}
}]; } /**
分享内容设置(网页)
注:此处包含多种分享种类样式的,具体根据需求选择具体方法;
方法实现大同小异,传递所需的参数和数据即可;
具体详见友盟接口中所对应的方法;
此 demo 是以分享网页链接为例,简单的图文介绍,点击后跳转进入预先设置好的 URL 链接.
@param platformType 平台类型(网页)
*/
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType AndCurrentViewController:(UIViewController *)controller {
// 创建分享消息对象
UMSocialMessageObject *messageObj = [UMSocialMessageObject messageObject];
// 创建网页内容对象
// NSString *thumURL = @"http://xxx.png";// 注:URL 链接地址必须为 https,具体详见 confitUShareSettings 方法中注释掉的 code 部分
NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
UIImage *thumImg = [UIImage imageNamed:icon];
UMShareWebpageObject *shareObj = [UMShareWebpageObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:thumImg];// 此处 thumImage 字段以当前 App 图标为例,若更换其它图片方法如此,支持 UIImage 或者 NSData 类型或者 image_url.
// 设置网页地址
shareObj.webpageUrl = @"http://xxxxxx";// 点击后欲跳转的 URL 链接地址
// 分享消息对象设置分享内容对象
messageObj.shareObject = shareObj; // 调用分享接口
kWeakSelf(self);
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObj currentViewController:controller completion:^(id result, NSError *error) {
if (error) {
NSLog(@"************Share fail with error *********\nError:%@", error);
}
else {
NSLog(@"************UMShare************\nResponse data is:%@", result);
} // Callback
[weakself callbackAlterStateWithError:error];
}];
} /**
分享内容设置(网页)
注:此处包含多种分享种类样式的,具体根据需求选择具体方法;
方法实现大同小异,传递所需的参数和数据即可;
具体详见友盟接口中所对应的方法;
此 demo 是以分享网页链接为例,简单的图文介绍,点击后跳转进入预先设置好的 URL 链接.
@param platformType 平台类型(网页)
@param controller 当前控件视图
@param dataSource 分享数据
*/
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType AndCurrentViewController:(UIViewController *)controller AndShareInfoData:(NSMutableDictionary *)dataSource {
/** 分享类型(base 则分享默认内容)*/
NSString *shareType = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"shareType"]]; NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
NSString *strTitle = @"分享标题";
NSString *strDescr = @"分享内容描述";
NSString *strWebpageUrl = @"分享链接地址";// 即:点击分享详情后跳转至相对应的 URL 链接地址 if ([shareType isEqualToString:@"custom"]) {// 分享类型:自定义
strTitle = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"title"]];
strDescr = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"descr"]];
strWebpageUrl = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"webpageUrl"]];
icon = [NSString stringWithFormat:@"%@", [dataSource objectForKey:@"imgUrl"]];// 注:该字段必须为 https,详见 confitUShareSettings 方法
if (kStringIsEmpty(icon)) {
icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
}
} // 创建分享消息对象
UMSocialMessageObject *messageObj = [UMSocialMessageObject messageObject];
// 创建网页内容对象
UIImage *thumImg = [UIImage imageNamed:icon];
UMShareWebpageObject *shareObj = [UMShareWebpageObject shareObjectWithTitle:strTitle
descr:strDescr
thumImage:thumImg];
// 设置网页地址
shareObj.webpageUrl = strWebpageUrl;
// 分享消息对象设置分享内容对象
messageObj.shareObject = shareObj; // 调用分享接口
kWeakSelf(self);
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObj currentViewController:controller completion:^(id result, NSError *error) {
if (error) {
NSLog(@"************Share fail with error *********\nError:%@", error);
}
else {
NSLog(@"************UMShare************\nResponse data is:%@", result);
} // Callback
[weakself callbackAlterStateWithError:error];
}];
} /**
回调分享状态
@param error 异常
*/
- (void)callbackAlterStateWithError:(NSError *)error {
NSString *callback = nil;
if (error) {
NSString *strError = [self callbackErrorWithErrorCode:error.code];
if (kStringIsEmpty(strError)) {
callback = @"分享失败";
}
else {
callback = strError;
} }
else {
callback = @"分享成功";
} // Show
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"欢迎使用【xxx App 名称】"
message:callback
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles: nil, nil];
[alert show];
} /**
回调异常提示
http://dev.umeng.com/wsq/android/errorcode @param code 异常代码
@return 异常提示
*/
- (NSString *)callbackErrorWithErrorCode:(NSInteger)code {
NSString *result = nil;
if (code == 10002) {
result = @"用户不存在";
}
else if (code == 10003) {
result = @"用户未登录";
}
else if (code == 10004) {
result = @"用户没有执行操作的权限";
}
else if (code == 10005) {
result = @"用户的id无效";
}
else if (code == 10006) {
result = @"用户已经被创建";
}
else if (code == 10007) {
result = @"已经关注过该用户";
}
else if (code == 10008) {
result = @"注册时用户信息不完整";
}
else if (code == 10009) {
result = @"用户不能关注自己";
}
else if (code == 10010) {
result = @"用户名长度超出范围,用户名为2~20个字符";
}
else if (code == 10011) {
result = @"用户不可用";
}
else if (code == 10012) {
result = @"用户名存在敏感词";
}
else if (code == 10013) {
result = @"用户已经存在";
}
else if (code == 10014) {
result = @"用户自定义字段从长度超出范围";
}
else if (code == 10015) {
result = @"该操作一次只能被一个用户操作";
}
else if (code == 10016) {
result = @"用户名存在非法字符";
}
else if (code == 10017) {
result = @"用户设备在黑名单中";
}
else if (code == 10018) {
result = @"该用户收藏 feed 数量最多50条";
}
else if (code == 10019) {
result = @"该 feed 已经被收藏";
}
else if (code == 10020) {
result = @"该 feed 还未被收藏";
}
else {
result = @"";
} return result;
} /*
case UMSocialPlatformErrorType_Unknow:
result = @"未知错误";
break;
case UMSocialPlatformErrorType_NotSupport:
result = @"不支持(url scheme 没配置,或者没有配置-ObjC, 或则SDK版本不支持或则客户端版本不支持";
break;
case UMSocialPlatformErrorType_AuthorizeFailed:
result = @"授权失败";
break;
case UMSocialPlatformErrorType_ShareFailed:
result = @"分享失败";
break;
case UMSocialPlatformErrorType_RequestForUserProfileFailed:
result = @"请求用户信息失败";
break;
case UMSocialPlatformErrorType_ShareDataNil:
result = @"分享内容为空";
break;
case UMSocialPlatformErrorType_ShareDataTypeIllegal:
result = @"分享内容不支持";
break;
case UMSocialPlatformErrorType_CheckUrlSchemaFail:
result = @"schemaurl fail";
break;
case UMSocialPlatformErrorType_NotInstall:
result = @"应用未安装";
break;
case UMSocialPlatformErrorType_Cancel:
result = @"您已取消分享";
break;
case UMSocialPlatformErrorType_NotNetWork:
result = @"网络异常";
break;
case UMSocialPlatformErrorType_SourceError:
result = @"第三方错误";
break;
case UMSocialPlatformErrorType_ProtocolNotOverride:
result = @"对应的 UMSocialPlatformProvider的方法没有实现";
break;
default:
*/ #pragma mark - Callback
// 兼容所有 iOS 设备
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
//注:该方法为建议使用的系统openURL回调,且 新浪 平台仅支持以上回调。还有以下两种回调方式,如果开发者选取以下回调,也请补充相应的函数调用。
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url
sourceApplication:sourceApplication
annotation:annotation];
if (!result) {
// 其他如支付等SDK的回调
} return result;
} //仅支持iOS9以上系统,iOS8及以下系统不会回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url
options:options];
if (!result) {
// 其他如支付等SDK的回调
} return result;
} - (BOOL)application:(UIApplication *)app handleOpenURL:(nonnull NSURL *)url {
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回调
} return result;
} @end 

最后,分享相关方法实现完毕后,便到了最终调用如上方法实现具体需求功能的时刻,定位到所需分享的代码块部分,调用之前预先预留好的接口方法即可.

#pragma mark - 缩写
#define kApplication [UIApplication sharedApplication]
#define kKeyWindow [UIApplication sharedApplication].keyWindow
#define kAppDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate)
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]
[kAppDelegate getUMShareRelevantMethodsWithCurrentViewController:self];

以上便是此次分享的全部内容,较为简易的分享小结,具体还以实际需求为准,可以自行 diy 调整,希望对大家有所帮助,也希望大神多多指点共进步!

最新文章

  1. OSG消息机制之消息分析
  2. CSS实现多个Div等高,类似表格布局
  3. js模仿新浪微博限制字数输入
  4. 怎么简单获取input file 选中的图片,并在一个div的img里面赋值src实现预览?
  5. 深入浅出设计模式——命令模式(Command Pattern)
  6. 【Tree 2】树形结构数据呈现的非递归算法(循环)实现
  7. CreateProcessAsUser,C#写的windows服务弹框提示消息或者启动子进程
  8. OpenCV的安装与系统环境变量
  9. C#实现局域网文件传输
  10. it小小鸟心得
  11. OTG
  12. PHP计划任务之关闭浏览器后仍然继续执行的函数 ignore_user_abort
  13. MIT6.828 JOS系统 lab2
  14. 用DMA直接驱动GPIO,实现GPIO最高输出速率(转)
  15. css里面position:relative与position:absolute的区别
  16. sublime text3中设置Emmet输入标签自动闭合
  17. nodejs yarn包管理工具
  18. Linux简单入门
  19. Centos 7 下 Corosync + Pacemaker + psc 实现 httpd 服务高可用
  20. 自动添加 Qt 开发生成的 exe 所需的依赖环境

热门文章

  1. Android中的AlertDialog遇到的错误
  2. IntelliJ IDEA里找不到javax.servlet的jar包
  3. WSGI学习系列WSME
  4. spring4 使用websocket
  5. Xtrareport绘制行号
  6. XtraReport三动态数据绑定
  7. Bootstrap学习笔记(四)
  8. JavaScript new 操作符 OOP(一)
  9. python面试题——网络编程和并发
  10. MongoDB之mongodb.cnf配置