ios应用之间的跳转

来源:互联网 发布:淘宝韩版男装 编辑:程序博客网 时间:2024/06/06 02:01

ios应用之间的跳转

iOS应用之间的跳转是通过URL Scheme实现的。

  1. URL Scheme

    ios应用将自身绑定到一个自定义的URL Scheme上,该Scheme用于从浏览器活其他应用中启动本应用。

  2. LSApplicationQueriesSchemes白名单

    判断系统是够安装了指定的APP,使用canOpenURL方法,ios9之前这个方法直接检测系统是否安装了APP,ios9之后为了用户的隐私安全,必须将APP设置成白名单之后才能通过canOpenURL的检测。

应用跳转demo

一、 应用B跳转到应用A

  1. 在应用A中添加URL Scheme(如:A)
  2. 在应用B的Info.plist中设置白名单,添加应用A的URL Scheme(A)

应用B中跳转代码:

- (IBAction)Tiaozhuan:(UIButton *)sender {    NSURL *url = [NSURL URLWithString:@"A://"];    //检测是否安装了应用A    if ([[UIApplication sharedApplication] canOpenURL:url]) {        //跳转到应用A        [[UIApplication sharedApplication] openURL:url];    }}

二、应用B跳转到应用A指定界面

应用B中代码:

- (IBAction)Tiaozhuan:(UIButton *)sender {    NSURL *url = [NSURL URLWithString:@"A://?vc=One"];    //检测是否安装了应用A    if ([[UIApplication sharedApplication] canOpenURL:url]) {        //跳转到应用A        [[UIApplication sharedApplication] openURL:url];    }}

应用A中代码:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {    if ([url.absoluteString rangeOfString:@"One"].location != NSNotFound) {        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;        OneViewController *vc = [OneViewController new];        [nav pushViewController:vc animated:YES];    }    return YES;}- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation {    if ([url.absoluteString rangeOfString:@"One"].location != NSNotFound) {        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;        OneViewController *vc = [OneViewController new];        [nav pushViewController:vc animated:YES];    }    return YES;}

三、应用A反跳回应用B

应用B中代码:

- (IBAction)Tiaozhuan:(UIButton *)sender {    NSURL *url = [NSURL URLWithString:@"A://B?vc=One"];    //检测是否安装了应用A    if ([[UIApplication sharedApplication] canOpenURL:url]) {        //跳转到应用A        [[UIApplication sharedApplication] openURL:url];    }}

应用A中代码:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {    if ([url.absoluteString rangeOfString:@"One"].location != NSNotFound) {        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;        OneViewController *vc = [OneViewController new];        vc.scheme = [[[[url.absoluteString componentsSeparatedByString:@"://"] lastObject] componentsSeparatedByString:@"?"] firstObject];        [nav pushViewController:vc animated:YES];    }    return YES;}- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation {    if ([url.absoluteString rangeOfString:@"One"].location != NSNotFound) {        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;        OneViewController *vc = [OneViewController new];        vc.scheme = [[[[url.absoluteString componentsSeparatedByString:@"://"] lastObject] componentsSeparatedByString:@"?"] firstObject];        [nav pushViewController:vc animated:YES];    }    return YES;}
@interface OneViewController : UIViewController@property (nonatomic, copy) NSString *scheme;@end- (void)back:(id)sender {    NSURL *url = [NSURL URLWithString:[self.scheme stringByAppendingString:@"://"]];    //此处不需要调用canOpen方法判断是否安装应用 直接跳转    [[UIApplication sharedApplication] openURL:url];}
0 0