美文网首页
俩个应用间的循环跳转

俩个应用间的循环跳转

作者: lee_moons | 来源:发表于2016-06-12 16:38 被阅读44次

应用跳转是根据协议头跳转

A跳转B,需要B增加URL Types ,A应用根据URL的协议头跳转

iOS8之前的跳转方法:
//适配iOS9 需要配置plist文件 -->添加应用白名单 -->info.plist添加LSApplicationQueriesSchemes数组

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];
}

//iOS9可以使用 不需要配置plist文件
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]]) {
    NSLog(@"打开失败");
}

分别跳转朋友圈和好友列表 给好友列表页面一个"唯一"的URL scheme以供跳转

在A中将scheme和要跳转B的协议头处理拼接
//headerStr是协议头 scheme是A的标识
urlStr = [NSString stringWithFormat:@"%@?%@",headerStr,scheme];

在B中处理传来的url
//lastUrl是截取出来的scheme
NSString *urlStr = [lastUrl stringByAppendingString:@"://"];

处理url得到scheme,并返回
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]]

A方

// 朋友圈
- (IBAction)timeline:(id)sender
{
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://timeline"]]) {
        NSLog(@"不能跳转到微信");
    }
    
}
// 好友列表
- (IBAction)session:(id)sender
{
//    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://session"]]) {
//        NSLog(@"不能跳转到微信");
//    }
    
    // 获取info.plist内容
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
//    NSLog(@"%@",info);
    // 获取scheme
    NSArray *urlTypes = info[@"CFBundleURLTypes"];
//    NSLog(@"%@",urlTypes);
    
    NSDictionary *dict = urlTypes[0];
    
    NSArray *urlSchemes = dict[@"CFBundleURLSchemes"];
    
    NSString *scheme = urlSchemes[0];
    NSLog(@"%@",scheme);
    
    // 拼接@"weixin://session" 和scheme
    NSString *headerStr = @"weixin://session";
    NSString *urlStr = [NSString stringWithFormat:@"%@?%@",headerStr,scheme];
     NSLog(@"%@",urlStr);
    // 跳转到好友列表
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]]) {
            NSLog(@"不能跳转到微信");
    }
}


B方

AppDelegate

@property (nonatomic, copy) NSString *lastStr;


#pragma mark 作用完全一样-->接收第三方app传递的信息
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    // weixin://session
    NSLog(@"%@",url);
    NSString *urlStr = url.absoluteString;
    // 获取 // 的范围
    NSRange range = [urlStr rangeOfString:@"//"];
    // 截取 // 后面的所有字符串
    // session?ert84tfv
    NSString *lastStr = [urlStr substringFromIndex:range.location + range.length];
    NSLog(@"%@",lastStr);
    self.lastStr = lastStr;
    
    // 获取根控制器
    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
    // 获取viewController
    ViewController *vc = nav.childViewControllers[0];
    // 回到根控制器
    [nav popToRootViewControllerAnimated:YES];
    // 跳转不同界面
    if ([lastStr containsString:@"session"]) {
        [vc performSegueWithIdentifier:@"session" sender:nil];
    }else if([lastStr containsString:@"timeline"]){
        [vc performSegueWithIdentifier:@"timeline" sender:nil];
    }
    return YES;
}

ViewController
点击返回到之前的项目

- (IBAction)backToApp:(id)sender
{
    // 获取应用程序的代理
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    // 获取//以后的字符串
    // lastStr:  session?ert84tfv
    NSString *lastStr = delegate.lastStr;
    // 获取?的范围
    NSRange range = [lastStr rangeOfString:@"?"];
    // 从?后面开始截取字符串
    NSString *scheme = [lastStr substringFromIndex:range.location + range.length];
    NSLog(@"%@",scheme);
    // 跳转
    if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:[scheme stringByAppendingString:@"://"]]]) {
        NSLog(@"不能跳转");
    }
}

相关文章

  • 俩个应用间的循环跳转

    应用跳转是根据协议头跳转 A跳转B,需要B增加URL Types ,A应用根据URL的协议头跳转 iOS8之前的跳...

  • 应用间跳转(Swift)

    一.应用跳转的介绍 1.应用间跳转即从一个应用程序跳转到另一个应用程序 2.应用间跳转的应用:1.应用推荐2.支付...

  • 应用间跳转、通信

    应用间跳转 •app应用的跳转的原理 •如何实现两个app应用之间的跳转 •如何实现两个app之间跳转到指定的页面...

  • 应用程序间跳转

    应用程序间跳转 1. 什么是应用间跳转,有什么作用? 2. 应用程序间跳转实现? 直接打开对应APP的scheme...

  • 应用程序间跳转和社交分享

    一. 应用程序间跳转 1. 什么是应用间跳转,有什么作用? 2. 应用程序间跳转实现? 直接打开对应APP的sch...

  • 应用间跳转

    iOS 9.0之后 应用程序跳转 需要设置白名单info.plist 增加 LSApplicationQueri...

  • 应用间跳转

    应用间跳转 应用场景使用第三方用户登录,需要用户授权,还需要“返回到调用的程序,同事返回授权的用户名”应用程序推广...

  • 应用间跳转

  • 应用间跳转

    本文简单介绍iOS应用程序互相跳转的方法 2.要打开本机上的其他应用程序,需要设置schemes,自定义的协议头,...

  • 应用间跳转

    实现应用间的跳转 从demo1到demo2,首先设置demo2 的 URL schme:设置路径 tagert -...

网友评论

      本文标题:俩个应用间的循环跳转

      本文链接:https://www.haomeiwen.com/subject/llgedttx.html