基本都能百度到:首先感谢作者的分享。。。。
iOS 请参考这里:https://www.jianshu.com/p/0811ccd6a65d
Android 请参考这里:https://www.jianshu.com/p/42ae7066f8f3
可以看看我的总结
iOS 篇 Scheme 方式
前期准备:
① 首先在App-2中注册 URLtypes 如:URL Schemes 为prob
② 然后在 App-1中的info.plist中添加允许打开App-2的请求
<key>LSApplicationQueriesSchemes</key>
<array>
<string>prob</string>
</array>
实现功能:
在App-1中需要打开App-2的地方实现
NSString *urlStr = @"prob://www.xxx.com?name=zhangsan&password=123";
NSURL *url = [NSURL URLWithString:urtStr];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else{
//无法打开app
}
在 App-2 中实现此方法,进行打印url的所有属性值
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options NS_AVAILABLE_IOS(9_0){
//在这里进行判断,实现不同功能
}
发现有值的如下:可能和我的url字符串有关系
url-scheme-------prob
url-host-------www.xxx.com
url-query-------name=zhangsan&password=123
总结:也就是说在App-2中需要根据传递过来的数据,进行判断实现不同的功能。。。
说明下:
[[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success) {
//...to do you want
}];
[[UIApplication sharedApplication] openURL:url]; //此方法在iOS10 已被 上面的方法所取代。所以请进行适配吧。。。
iOS 篇 bundle ID 方式
/// 通过bundleId打开App
/// @param bid NSString
+ (BOOL)openAppByBundleId:(NSString *)bid{
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject * workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
BOOL isOpen = [workspace performSelector:@selector(openApplicationWithBundleID:) withObject:bid];
return isOpen;
}
如何获取bundleID
/// 获取所有的应用
+ (void)allApplications{
// Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
// NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
// NSLog(@"apps: %@", [workspace performSelector:@selector(allApplications)]);
if (@available(iOS 12.0, *)) {
Class LSApplicationWorkspace_Class = NSClassFromString(@"LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace_Class performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *plugins = [workspace performSelector:NSSelectorFromString(@"installedPlugins")];
for (id plugin in plugins) {
id bundle = [plugin performSelector:NSSelectorFromString(@"containingBundle")];
if (bundle) {
NSString *itemName = [bundle performSelector:NSSelectorFromString(@"itemName")];
if (itemName) {
NSLog(@"%@ <%@>", [bundle performSelector:NSSelectorFromString(@"bundleIdentifier")], [bundle performSelector:NSSelectorFromString(@"itemName")]);
}
}
}
}
}
Android 篇 包名方式
Android appA打开另一个appB
首先需要知道appB的包名,以及直接打开的Activity,
并且在appB的清单文件中将属性exported设置为true
使用以下代码即可,打开
public static final String packname = "com.example.lxf.openb";
/**
* 直接打开AppB
* @param view
*/
public void openB(View view){
//进行打开appB的操作
PackageManager packageManager = getPackageManager();
if (checkPackInfo(packname)) {
Intent intent = packageManager.getLaunchIntentForPackage(packname);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "没有安装" + packname, Toast.LENGTH_LONG).show();
}
}
/**
* 打开B的某个页面
* @param view
*/
public void openActivityInB(View view){
//进行打开appB的操作
PackageManager packageManager = getPackageManager();
if (checkPackInfo(packname)) {
Intent intent = new Intent();
//第一种方式
ComponentName cn = new ComponentName(packname, "com.example.lxf.openb.InfoActivity");
try {
intent.setComponent(cn);
//第二种方式
//intent.setClassName(packname, "com.example.lxf.openb.InfoActivity");
intent.putExtra("test", "intent1"); //将需要的东西传递
startActivity(intent);
} catch (Exception e) {
//TODO 可以在这里提示用户没有安装应用或找不到指定Activity,或者是做其他的操作
}
} else {
Toast.makeText(MainActivity.this, "没有安装" + packname, Toast.LENGTH_LONG).show();
}
}
/**
* 检查包是否存在
*
* @param packname
* @return
*/
private boolean checkPackInfo(String packname) {
PackageInfo packageInfo = null;
try {
packageInfo = getPackageManager().getPackageInfo(packname, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return packageInfo != null;
}
网友评论