iOS应用间跳转

近期在做两个应用之间的跳转,合作伙伴提供了这篇文章,觉得不错,在这样分享,大家一起学习

文/船长_(简书作者)
原文链接:http://www.jianshu.com/p/732c5e1720d0
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。


应用跳转.png
  • 场景需求:一个应用A(以news应用为示例)跳转到另外一个应用B(以weChat为示例),常见需求如下
    • 1.应用推荐
    • 2.支付宝支付
    • 3.第三方登录
    • 4.微信分享
  • 注意:iOS9中打开一个应用程序的URL必须配置info.plist文件
    • 添加LSApplicationQueriesSchemes的key
    • 添加对应url的scheme

一: 打开系统的应用程序

打开打电话应用程序
URL:tel://电话号码
打开发短信应用程序
URL:sms://电话号码
打开系统的设置界面,必须先在info.plist中配置URL SchemesURL Types中添加prefs
打开Wifi设置
URL:prefs:root=WIFI
打开定位服务
URL:prefs:root=LOCATION_SERVICES
打开蓝牙服务
URL:prefs:root=Bluetooth
打开FaceTime
URL:prefs:root=FACETIME
打开音乐
URL:prefs:root=MUSIC
打开墙纸设置
URL:prefs:root=Wallpaper

配置如图下图


sc.png
  • 分析:核心代码:
    [[UIApplication sharedApplication] openURL:url]

    一:应用A跳转应用B

    1.界面搭建如图


    设置.png

    2.A应用打开B应用的方法

- (IBAction)jumpToweChat:(id)sender {

    NSURL *url = [NSURL URLWithString:@"weixin://"];

    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}
- (IBAction)jumpToFriends:(id)sender {
    NSURL *url = [NSURL URLWithString:@"weixin://session?news"];

    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}

- (IBAction)jumpToTimeLIne:(id)sender {
    NSURL *url = [NSURL URLWithString:@"weixin://timeLine?news"];

    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}
2.设置应用A和应用B的URL Types中的URL Schemes

A.png

B.png

3.在应用B中监听跳转,进行判断,执行不同的跳转

  • 在AppDelegate中实现下面的方法监听
// 已过期
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
// 已过期
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
/**
 *  通过别的应用打开我们的应用时会来到这里
 *  @param url               通过什么URL打开的
 *  @param sourceApplication 打开我们应用的sourceApplication
 */
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    // 1.获取通过那一个URL打开我的应用程序
    NSString *urlStr = url.absoluteString;

    // 2.取出window的根控制器
    UINavigationController *rootNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    // 首先回到根控制器
    [rootNav popToRootViewControllerAnimated:NO];

    // 3.取出MainViewController,使用主要控制器就可以跳转到另外两个控制器
    ViewController *rootVc = rootNav.childViewControllers.firstObject;

    // 传值
    rootVc.urlPath = urlStr;

    if ([urlStr containsString:@"session"]) { // 好友界面
        [rootVc performSegueWithIdentifier:@"session" sender:nil];

    }else if ([urlStr containsString:@"timeLine"]) {// 朋友圈界面
        [rootVc performSegueWithIdentifier:@"timeLine" sender:nil];
    }

    return YES;
}

4.从应用B跳转到应用A

// 这里用storyboard做的,监听程序的跳转
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"session"]) {

        SessionViewController *vc = segue.destinationViewController;
        vc.urlPath = self.urlPath;
    }else if ([segue.identifier isEqualToString:@"timeLine"]) {

        TimeLineViewController *vc = segue.destinationViewController;
        vc.urlPath1 = self.urlPath;
    }
}

5.微信朋友圈界面设置

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"微信朋友圈";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
}

- (void)clickLeftButton
{
    // 截取字符串,拿到scheme
    NSInteger location = [self.urlPath1 rangeOfString:@"?"].location;
    NSString *scheme = [self.urlPath1 substringFromIndex:location + 1];

    // 通过scheme返回新闻
    NSString *news = [NSString stringWithFormat:@"%@://", scheme];
    NSURL *newsUrl = [NSURL URLWithString:news];

    if ([[UIApplication sharedApplication] canOpenURL:newsUrl]) {
        [[UIApplication sharedApplication] openURL:newsUrl];
    }
}

6.微信好友界面设置

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"微信好友界面";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
}

- (void)clickLeftButton
{
    // 截取字符串,拿到scheme
    NSInteger location = [self.urlPath rangeOfString:@"?"].location;
    NSString *scheme = [self.urlPath substringFromIndex:location + 1];

    // 通过scheme返回新闻
    NSString *news = [NSString stringWithFormat:@"%@://", scheme];
    NSURL *newsUrl = [NSURL URLWithString:news];

    if ([[UIApplication sharedApplication] canOpenURL:newsUrl]) {
        [[UIApplication sharedApplication] openURL:newsUrl];
    }
}

示例图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值