问题:
最近在做ShareSDK分享的时候遇到了分享的时候手机上没有安装微信或者QQ等客户端的情况,想要在项目内弹一个让用户安装客户端的提示。
提示:
ios是基于ShareSDK的判断没有尝试在没有ShareSDK的时候能不能用,安卓是通用的判断判断本地包名
安卓相关代码:
/**
* 判断是否安装了微信
* @param context
* @return
*/
public static boolean isWeixinInstalled(Context context) {
final PackageManager packageManager = context.getPackageManager();
// 获取所有已安装程序的包信息
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName.toLowerCase(Locale.ENGLISH);
if (pn.equals("com.tencent.mm")) {
return true;
}
}
}
return false;
}
/**
* 判断是否安装了QQ
* @param context
* @return
*/
public static boolean isQQClientInstalled(Context context) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName.toLowerCase(Locale.ENGLISH);
if (pn.equals("com.tencent.mobileqq")) {
return true;
}
}
}
return false;
}
/**
* 判断是否安装了微博
* @param context
* @return
*/
public static boolean isWeiboInstalled(Context context) {
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName.toLowerCase(Locale.ENGLISH);
if (pn.equals("com.sina.weibo")) {
return true;
}
}
}
return false;
}
/**
* 检查支付宝是否存在
* @param context
* @return
*/
public boolean isAlipayInstalled(Context context) {
PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
System.out.println(pinfo.get(i).packageName);
if (pn.equals("com.eg.android.AlipayGphone")) {
return true;
}
}
}
return false;
}
Ios相关代码
//导入头文件
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
//判断逻辑
// 判断是否安装微信
+(BOOL)ISWeixinInstalled {
if ([WXApi isWXAppInstalled] )
{
//判断当前微信的版本是否支持OpenApi
if ([WXApi isWXAppSupportApi])
{
NSLog(@"安装了微信");
return YES;
}
else
{
NSLog(@"请升级微信至最新版本!");
return NO;
}
}
else
{
NSLog(@"请安装微信客户端");
return NO;
}
}
// 判断是否安装QQ
+(BOOL)ISQQInstalled {
if ([QQApiInterface isQQInstalled] )
{
//判断当前QQ的版本是否支持OpenApi
if ([QQApiInterface isQQSupportApi])
{
NSLog(@"安装了QQ");
return YES;
}
else
{
NSLog(@"请升级QQ至最新版本!");
return NO;
}
}
else
{
NSLog(@"请安装QQ客户端");
return NO;
}
}
|