iOS应用之间的跳转和数据传

ios中,如果我们想要从一个应用程序跳转到另一个应用程序怎么办,加入你的手机上同时安装了淘宝和支付宝两个应用程序,你点击支付的时候,手机会自动打开手机上安装的应用支付宝,这个功能如何实现。且看:

假如创建了两个项目,demoA,demoB(省去创建过程),想要在demoA中打开demoB,

1.需要在demoB中设置如下:

2,在demoA中添加如下,

    NSURL *url = [NSURL URLWithString:@"appb://name=iPhone6&price=5288"];

    //跳转前先判断,是否可以打开链接

    if ([[UIApplication sharedApplication] canOpenURL:url] == YES)

    {

        [[UIApplication sharedApplication] openURL:url];

    }

    else

    {

        NSLog(@"连接不能打开,应用程序未安装");

    }

 

3.在demoB中进行接收处理,如果是第一次启动demoB会调用didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

     NSLog(@"launchOptions %@",launchOptions);

    return YES;

}

 

//当一个应用程序被其他程序打开的时候会调用这个方法,在该方法中可以实现两个应用程序间的数据局传递
 -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
 {
       NSLog(@"url:%@",url); 
   NSLog(@"source Application:%@",sourceApplication);
   return YES;
}

log如下:

 Oct 13 19:47:18 iPhone DemoB[894] <Warning>: launchOptions {

UIApplicationLaunchOptionsSourceApplicationKey = "com.test.DemoA";
UIApplicationLaunchOptionsURLKey = "appb://name=iPhone6&price=5288";
}
Oct 13 19:47:18 iPhone DemoB[894] <Warning>: url:appb://name=iPhone6&price=5288
Oct 13 19:47:18 iPhone DemoB[894] <Warning>: source Application:com.test.DemoA

over!

原文地址:https://www.cnblogs.com/menchao/p/4876823.html