记录下,当别人要求唤起某个应用,但没提供太多信息(除了应用名称之外)的情况下,可能会用的上。
第一步,查看包名和Scheme,需要下载对应App的apk,ipa。
Android:解压apk,找出AndroidManifest.xml文件。使用AXMLPrinter2.jar对xml文件进行反编译。(环境要求:能运行java命令,在网上下载AXMLPrinter2.jar)
编译命令:
java -jar AXMLPrinter2.jar AndroidManifest.xml > main.txt
运行完毕后得到的main.txt里就是反编译后的明文xml。在其中找scheme和package就行;其中scheme可能有多个,你需要逐个去试下能不能唤起对应应用的期望界面;(按经验来说,含有“notification”字样的scheme大多符合唤起界面的要求)
IOS:将ipa移至macbook上,把后缀名改成zip,利用归档工具解压;点进去,有个“显示包内容”选项,里面找到info.plist文件;URL Types里找对应的URL Scheme就行;包名的话,就找“CFBundleIdentifier”这个字段对应的值;
第二步,编写唤起代码。(例子:包名:com.aaa.bbb;scheme:xxx)
Android应用: 根据包名唤起的方法:openAppcation("com.aaa.bbb");
public static boolean openAppcation(String packname){
try {
PackageManager packageManager = mContext.getPackageManager();
Intent intent = new Intent();
intent = packageManager.getLaunchIntentForPackage(packname);
if (intent != null) {
mContext.startActivity(Intent.createChooser(intent, ""));
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
根据scheme唤起:
public static void openXXX() {
try {
PackageManager packageManager = mContext.getPackageManager();
Intent intent = new Intent();
intent = packageManager.getLaunchIntentForPackage("com.aaa.bbb");
if (intent == null) {
//Toast提示之类("请安装XXX客户端");
} else {
mContext.startActivity(Intent.createChooser(intent, ""));
}
} catch (Exception e) {
e.printStackTrace();
}
}
手机网页:根据scheme唤起:(TS版)
window.open("xxx://");
IOS内应用:根据包名唤起:[openAppcation:@"com.aaa.bbb"];
#import <objc/runtime.h> //objc_getClass 用到
//唤起应用
+(NSString *)openAppcation:(NSString *)packname{
Class lsawsc = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
// iOS6 没有defaultWorkspace
if ([workspace respondsToSelector:NSSelectorFromString(@"openApplicationWithBundleID:")])
{
[workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:packname];
return @"1";
}
//0 失败 1 成功 2其他平台默认
return @"0";
}
根据URL Scheme唤起:
//打开XXX
+(void) openXXX:(NSString *)num{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"xxx://"]]) {
NSURL *url1 = [NSURL URLWithString:@"xxx://"];
[[UIApplication sharedApplication] openURL:url1 options:@{} completionHandler:^(BOOL success) {
if(success)
NSLog(@"open xxx successed");
else{
NSLog(@"open xxx failed");
}
}];
}else{
NSLog(@"open xxx failed");
//或者跳到APPSTORE里下载该应用
//[XXX jumpAppStoreDL:@"该应用的appid"];
}
}
//跳转app store 下载应用
+(void) jumpAppStoreDL:(NSString *)appid{
NSString *url = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appid];
//应用 在App Store的 app ID
NSURL *url2 = [NSURL URLWithString:url];
if (@available(iOS 10.0, *)){
[[UIApplication sharedApplication]openURL:url2 options:@{UIApplicationOpenURLOptionsSourceApplicationKey:@YES} completionHandler:^(BOOL success) {
if (success) {
NSLog(@"10以后可以跳转 App Store下应用 id:%@",appid);
}else{
NSLog(@"10以后不可以跳转App Store下应用 id:%@",appid);
}
}];
}else{
BOOL success = [[UIApplication sharedApplication]openURL:url2];
if (success) {
NSLog(@"10以前可以跳转 App Store下应用 id:%@",appid);
}else{
NSLog(@"10以前不可以跳转 App Store 下应用 id:%@",appid);
}
}
}
并且还要在你应用的Info.plist里白名单(LSApplicationQueriesSchemes)加上它的URL Scheme。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>xxx</string>
<string>itms-apps</string>
<string>weixinULAPI</string>
<string>weixin</string>
<string>wechat</string>
<string>mqq</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqqapi</string>
<string>mqqopensdkapiv2</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV4</string>
<string>mqzoneopensdk</string>
<string>mqzoneopensdkapiV2</string>
<string>mqqopensdkminiapp</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqOpensdkSSoLogin</string>
</array>
至此,记录完毕。😄