应用程序跳转详解

作者: 鲲鹏DP | 来源:发表于2016-08-16 23:12 被阅读441次

1.跳转到另一个程序的主界面

  • 每个程序都该有一个对应的Scheme,以确定对应的url


    Snip20160816_2.png
  • 一个程序要跳转到(打开)另外一个程序,需要将另外一个程序的Scheme添加到自己的应用程序白名单中(在info.plist中配置:LSApplicationQueriesSchemes,类型为数组,在数组中添加相应的Scheme)->ios9.0开始


    Snip20160816_3.png
  • 跳转代码
extension ViewController {
    
    @IBAction func jumpToXinWen(sender: AnyObject) {
        openURL("xinWen://")
        
    }
    private func openURL (urlString : String) {
        let url = NSURL(string: urlString)!
        if UIApplication.sharedApplication().canOpenURL(url) {
            UIApplication.sharedApplication().openURL(url)
        }
        
    }
}

2.跳转到另一个程序的指定界面

  • 完成上面程序间跳转的相应设置
  • 实现跳转代码(与跳转到主页相比,url多了参数,?前面参数是目标程序想要跳转界面的segu标签,?后面是当前程序的scheme)
  // MARK: - 跳转微信朋友圈
    @IBAction func jumpToWeChatTimeLine(sender: AnyObject) {
        openURL("WeChat://TimeLine?xinWen")
        
    }
   // MARK: - 跳转微信好友
    @IBAction func jumpToWeChatSession(sender: AnyObject) {
        openURL("WeChat://Session?xinWen")
    
    }
    private func openURL (urlString : String) {
        let url = NSURL(string: urlString)!
        if UIApplication.sharedApplication().canOpenURL(url) {
            UIApplication.sharedApplication().openURL(url)
        }
       
  • 在目标程序AppDelegate中监听用来跳转的相应信息,根据这些信息让目标程序自己实现页面切换
extension AppDelegate {
    //监听当前程序被其他程序通过什么样的Url打开
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        //根据url跳转对应页面
        //1.url转化成字符串
        let urlString = url.absoluteString
        //2.获取首页控制器
        let rootVc = application.keyWindow?.rootViewController
        let mainVc = rootVc?.childViewControllers[0] as! ViewController
          //将url传递给mianVc
        mainVc.urlString = urlString
        //3.根据字符串内容完成对应跳转
        if urlString.containsString("Session") {//跳转好友
            mainVc.performSegueWithIdentifier("Session", sender: nil)
        }else if urlString.containsString("TimeLine") {//跳转朋友圈
            mainVc.performSegueWithIdentifier("TimeLine", sender: nil)
        }
        return true
    }
}

3.如何从目标程序的非主页界面回到当前(跳转前)程序呢?

思路: 只要在目标程序的非主页界面知道跳转前的程序的URL即可直接跳转,所以,这里的关键是如何将跳转前的程序的URL传递到目标程序的非主页界面.

  • 在目标控制器APPDelegate中能获取到用来跳转的URl信息的方法中将url传递给mianVC(事先定义好接收数据的属性),如上面代码所示.
  • 在mianVc 中将url传递给需要切换的控制器(事先定义好接收数据的属性)
    //切换界面,需要来到该方法.能够拿到切换前后的控制器
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        if segue.identifier == "Session" {
            let sessionVc = segue.destinationViewController as! SessionViewController
            //传递数据
            sessionVc.urlString = urlString
        }
    }
}
  • 在目标控制器中根据url信息,获取跳转前控制器的scheme,从而得到跳转回去的url.
class SessionViewController: UIViewController {
    
    //接收数据
 var urlString = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "退回跳前应用", style: .Plain, target: self, action: #selector(backToStartApp))
        
    }

}
extension SessionViewController {
    func backToStartApp() {
        //分割Url,获取跳转前的程序的scheme
    
        let scheme = urlString.componentsSeparatedByString("?")[1]
      print(scheme)
        //拼接字符串
        let backString = "\(scheme)://"
        //打开url
        openURL(backString)
    }
    
    
    private func openURL (urlString : String) {
        let url = NSURL(string: urlString)!
        if UIApplication.sharedApplication().canOpenURL(url) {
            UIApplication.sharedApplication().openURL(url)
        }
        
    }

}

相关文章

  • 应用程序跳转详解

    1.跳转到另一个程序的主界面 每个程序都该有一个对应的Scheme,以确定对应的urlSnip20160816_2...

  • 4.2UINavigationController详解2和视图的

    UINavigationController详解2 视图的模态跳转

  • 应用程序间跳转

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

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

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

  • 应用程序之间的跳转

    首先要在跳转应用程序中button的连线事件中: 其中speechWords://是即将跳转应用程序的URL Ty...

  • 应用间跳转(Swift)

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

  • reactnative ~ android 模块通讯混合跳转方案

    rn ~ android 模块通讯混合跳转方案详解 android原生 接入rn模块 原生接入rn + 通信详解资...

  • iOS应用程序之间的跳转

    有时候我们的APP需要跳转到别的应用程序中去: 我们只需要知道要跳转去的应用程序的应用标识就可以做跳转了 配置程序...

  • 应用跳转

    应用程序跳转 从weixin跳转到QQ1、给应用程序配置URL2、URL:协议头+路径(URL可以没有路径)3、相...

  • 2019-01-09【PHP跳转页面】

    PHP跳转页面的几种实现方法详解 •PHP页面跳转一、header()函数 header()函数是PHP中进行页面...

网友评论

    本文标题:应用程序跳转详解

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