我记得上一次使用 Mob 还是在 2017年 的时候,当时可能是做了一个分享的功能,一回头都过去5年多了… 没想到新公司的新项目,沿用的是 Mob 提供的 三方登录 和 三方分享 ,有些怀念的感觉,所以记录一下逝去的时光 ~
一切均以 Mob 官方文档 为准,毕竟作为开发者要按照平台要求去集成
平台注册 - 配置信息
- 在 Mob 找到
技术文档 - 开始集成指导
- Mob 文档中心 - 集成ShareSDK
- 创建应用
将我们的应用首先注册到Mob平台,因为SDK初始化时需要提供这些配置信息 - 内部包含:AppID、AppSecret
创建 - 结果示例
内部包含:AppID、AppSecret
- 第三方平台注册指南
不论是三方登录、三方分享,本质都是我们app需要跳转到三方,调用一些数据或者功能,所以需要注册三方平台的账户,例如QQ、微信等等 → 请到对应的三方平台进行账户、应用注册
创建 - 结果示例
内部包含:AppID、AppSecret
项目实战 - 开始集成
关于通过Mob集成三方的方式,主要以 快速开始 - Android快速集成、客户端SDK - 集成指南 为准!!!
此篇生效结果时间 - 2022年8月19日
在集成的过程中,官网提供这张图挺形象的,有利于了解其背后的实现过程
gradle 配置
build.gradle(Project)
buildscript {
repositories {
maven {
url "https://mvn.mob.com/android"
}
...
}
dependencies {
...
classpath "com.mob.sdk:MobSDK:2018.0319.1724"
}
}
build.gradle(app)
MobSDK {
appKey "mob注册app的key"
appSecret "mob注册app的appSecret"
ShareSDK {
loopShare true
ffmpeg true
devInfo {
Wechat {
id 1
sortId 1
appId "微信注册app的key"
appSecret "微信注册app的appSecret"
callbackUri "http://sharesdk.cn"
userName "小程序的原始ID"
path "小程序内的页面路径"
withShareTicket true
miniprogramType 0
bypassApproval false
enable true
}
}
}
}
- 在
gradle.properties 中添加代码
MobSDK.spEdition=FP
图示
代码混淆
ShareSDK已经做了混淆处理,再次混淆会导致不可预期的错误,请在你的混淆脚本中添加如下的配置,跳过对ShareSDK的混淆操作
实战使用
我们经常集成三方平台的SDK,大多都是Application内进行初始化,但是Mob这方便可能有点小不同
Android这些年对用户的隐私保护越来越高 ,对开发的兼容适配要求也越来越高 ,所以在调用 Mob SDK 前,最好让用户确定隐私授权 ,Mob 是这样告知告知开发者的
接口调用 回传用户隐私授权结果(submitPolicyGrantResult ) 为保证您的App在集成MobSDK之后能够满足工信部相关合规要求 ,您应确保App安装首次冷启动且取得用户阅读您《隐私政策》授权 之后,调用MobSDK.submitPolicyGrantResult回传隐私协议授权结果 。 反之,如果用户不同意您App《隐私政策》授权,则不能调用MobSDK.submitPolicyGrantResult回传隐私协议授权结果 。 相关隐私声明请参考合规指南
故我认知为有以下几个场景可以直接进行MobSDK.submitPolicyGrantResult 初始化
用户同意隐私协议 后设置 MobSDK.submitPolicyGrantResult(true);
Application 内直接设置 MobSDK.submitPolicyGrantResult(true); 调用三方登录、三方分享 时设置 MobSDK.submitPolicyGrantResult(true);
三方登陆
mob的三方登陆可以直接 → 在鉴权完成之后,获取到用户信息,以及 token
fun weChatMobLogin(context: Activity, listener: WxListener) {
MobSDK.submitPolicyGrantResult(true, null)
val wechat = ShareSDK.getPlatform(Wechat.NAME)
if (wechat.isClientValid) {
Log.d("ShareSDK", "可用客户端")
}else{
Toast.makeText(context, "微信未安装,请先安装微信", Toast.LENGTH_LONG).show();
}
if (wechat.isAuthValid) {
wechat.removeAccount(true);
}
wechat.platformActionListener = object : PlatformActionListener {
override fun onComplete(platform: Platform, i: Int, hashMap: HashMap<String?, Any?>?) {
listener.onComplete(platform.db.token)
Log.d("ShareSDK", "onComplete ----> 登录成功" + platform.db.exportData())
}
override fun onError(platform: Platform?, i: Int, throwable: Throwable) {
Log.d("ShareSDK", "onError ----> 登录失败$throwable")
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.stackTrace.toString())
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.message)
}
override fun onCancel(platform: Platform?, i: Int) {
Log.d("ShareSDK", "onCancel ----> 登录取消")
}
}
wechat.SSOSetting(false)
wechat.showUser(null)
ShareSDK.setActivity(context);
wechat.authorize()
}
三方分享
内部的俩个方法没啥区别,都能用
fun shareWx() {
val sp = Platform.ShareParams()
sp.title = "测试分享的标题"
sp.titleUrl = "http://sharesdk.cn"
sp.text = "测试分享的文本"
sp.imageUrl = "http://www.someserver.com/测试图片网络地址.jpg"
sp.site = "发布分享的网站名称"
sp.siteUrl = "发布分享网站的地址"
val wx = ShareSDK.getPlatform(Wechat.NAME)
wx.platformActionListener = object : PlatformActionListener {
override fun onError(arg0: Platform, arg1: Int, arg2: Throwable) {
Log.d("ShareSDK", "分享失败的回调")
}
override fun onComplete(
arg0: Platform?,
arg1: Int,
p2: java.util.HashMap<String, Any>?,
) {
Log.d("ShareSDK", "分享成功的回调")
}
override fun onCancel(arg0: Platform, arg1: Int) {
Log.d("ShareSDK", "取消分享的回调")
}
}
wx.share(sp)
}
fun wechatShare() {
MobSDK.submitPolicyGrantResult(true);
val sp = Platform.ShareParams()
sp.title = "开心就好"
sp.text = "测试"
sp.url = "http://f.moblink.mob.com/pro/scene/novel/?id=0&mobid=7670310798648901632""
sp.imageUrl = "分享图片.png"
sp.shareType = Platform.SHARE_WEBPAGE
val WX = ShareSDK.getPlatform(Wechat.NAME)
WX.platformActionListener = object : PlatformActionListener {
override fun onComplete(
platform: Platform?,
i: Int,
hashMap: java.util.HashMap<String?, Any?>?,
) {
Log.d("ShareSDK", "onComplete ----> 分享成功")
}
override fun onError(platform: Platform, i: Int, throwable: Throwable) {
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.stackTrace.toString())
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.message)
}
override fun onCancel(platform: Platform, i: Int) {
Log.d("ShareSDK", "onCancel ----> 分享取消")
}
}
WX.share(sp)
}
封装工具
package com.xxx.xx.utils
import android.app.Activity
import android.content.Context
import android.util.Log
import android.widget.Toast
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.framework.ShareSDK
import cn.sharesdk.onekeyshare.OnekeyShare
import cn.sharesdk.wechat.friends.Wechat
import com.mob.MobSDK
object WxPlatformUtil {
interface WxListener {
fun onComplete(code:String)
}
fun weChatMobLogin(context: Activity, listener: WxListener) {
MobSDK.submitPolicyGrantResult(true, null)
val wechat = ShareSDK.getPlatform(Wechat.NAME)
if (wechat.isClientValid) {
Log.d("ShareSDK", "可用客户端")
}else{
Toast.makeText(context, "微信未安装,请先安装微信", Toast.LENGTH_LONG).show();
}
if (wechat.isAuthValid) {
wechat.removeAccount(true);
}
wechat.platformActionListener = object : PlatformActionListener {
override fun onComplete(platform: Platform, i: Int, hashMap: HashMap<String?, Any?>?) {
listener.onComplete(platform.db.token)
Log.d("ShareSDK", "onComplete ----> 登录成功" + platform.db.exportData())
}
override fun onError(platform: Platform?, i: Int, throwable: Throwable) {
Log.d("ShareSDK", "onError ----> 登录失败$throwable")
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.stackTrace.toString())
Log.d("ShareSDK", "onError ----> 登录失败" + throwable.message)
}
override fun onCancel(platform: Platform?, i: Int) {
Log.d("ShareSDK", "onCancel ----> 登录取消")
}
}
wechat.SSOSetting(false)
wechat.showUser(null)
ShareSDK.setActivity(context);
wechat.authorize()
}
fun shareWx() {
val sp = Platform.ShareParams()
sp.title = "测试分享的标题"
sp.titleUrl = "http://sharesdk.cn"
sp.text = "测试分享的文本"
sp.imageUrl = "http://www.someserver.com/测试图片网络地址.jpg"
sp.site = "发布分享的网站名称"
sp.siteUrl = "发布分享网站的地址"
val wx = ShareSDK.getPlatform(Wechat.NAME)
wx.platformActionListener = object : PlatformActionListener {
override fun onError(arg0: Platform, arg1: Int, arg2: Throwable) {
Log.d("ShareSDK", "分享失败的回调")
}
override fun onComplete(
arg0: Platform?,
arg1: Int,
p2: java.util.HashMap<String, Any>?,
) {
Log.d("ShareSDK", "分享成功的回调")
}
override fun onCancel(arg0: Platform, arg1: Int) {
Log.d("ShareSDK", "取消分享的回调")
}
}
wx.share(sp)
}
var shareUrl = "http://f.moblink.mob.com/pro/scene/novel/?id=0&mobid=7670310798648901632"
fun wechatShare() {
MobSDK.submitPolicyGrantResult(true);
val sp = Platform.ShareParams()
sp.title = "开心就好"
sp.url = shareUrl
sp.imageUrl = "分享图片.png"
sp.shareType = Platform.SHARE_WEBPAGE
val WX = ShareSDK.getPlatform(Wechat.NAME)
WX.platformActionListener = object : PlatformActionListener {
override fun onComplete(
platform: Platform?,
i: Int,
hashMap: java.util.HashMap<String?, Any?>?,
) {
Log.d("ShareSDK", "onComplete ----> 分享成功")
}
override fun onError(platform: Platform, i: Int, throwable: Throwable) {
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.stackTrace.toString())
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.message)
}
override fun onCancel(platform: Platform, i: Int) {
Log.d("ShareSDK", "onCancel ----> 分享取消")
}
}
WX.share(sp)
}
private fun showShare(platform: String?) {
val oks = OnekeyShare()
if (platform != null) {
oks.setPlatform(platform)
}
oks.setTitle("标题")
oks.setTitleUrl("http://sharesdk.cn")
oks.text = "我是分享文本"
oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg")
oks.setUrl("http://sharesdk.cn")
oks.show(MobSDK.getContext())
}
}
使用方式
WxPlatformUtil.weChatMobLogin(this,object : WxPlatformUtil.WxListener {
override fun onComplete(code: String) {
code.isNullOrEmpty().let {
if (!it){
mViewModel.wxLogin(code)
}
}
}
})
WxPlatformUtil.wechatShare()
|