app间跳转传参【iOS android】

iOS
A->B
A: 1.在iOS9中,如果使用 canOpenURL:方法,该方法所涉及到的 URL Schemes 必须在"Info.plist"中将它们列为白名单,否则不能使用。key叫做LSApplicationQueriesSchemes ,键值内容是对应应用程序的URL Schemes。(test)
2.代码:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@“test://Page1”]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“test://”] options:nil completionHandler:nil];
}else{
NSLog(@“尚未安装应用”);
//可以跳商店去
}

B:
targets -> Info -> URL Types -> 填写scheme
命名一个名字test
代码:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    // 1.获取导航栏控制器
    UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
    // 2.获得主控制器
    ViewController *mainVc = [rootNav.childViewControllers firstObject];

    // 3.每次跳转前必须是在跟控制器(细节)
    [rootNav popToRootViewControllerAnimated:NO];   

    // 4.根据字符串关键字来跳转到不同页面
    if ([url.absoluteString containsString:@"Page1"]) { // 跳转到应用App-B的Page1页面
        // 根据segue标示进行跳转
        [mainVc performSegueWithIdentifier:@"homeToPage1" sender:nil];
    } else if ([url.absoluteString containsString:@"Page2"]) { // 跳转到应用App-B的Page2页面
        // 根据segue标示进行跳转
        [mainVc performSegueWithIdentifier:@"homeToPage2" sender:nil];
    }   

    return YES;

android

A:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

              if (checkApkExist(MainActivity.this,"free.com.bproject")){
                    Intent intent = new Intent();
                    intent.setClassName("free.com.bproject", "free.com.bproject.MainActivity");
                    Bundle bundle = new Bundle();
                    bundle.putString("FIRST_APP_KEY", "this is a app send message");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Toast.makeText(MainActivity.this,"应用未安装",Toast.LENGTH_SHORT).show();

                }




            }
        });
    }
    public static boolean checkApkExist(Context context, String packageName){
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES);

            return true;
        } catch (PackageManager.NameNotFoundException e) {

            return false;
        }
    }

B:

  <activity
            android:name=".MainActivity"
            android:exported="true"
            >

 Bundle bundle = this.getIntent().getExtras();
    if (bundle != null){ //防止直接启动MainActivity时空指针闪退
        String value = bundle.getString("FIRST_APP_KEY");
        TextView textView = findViewById(R.id.textview);
        textView.setText(value);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值