应用程序跳转详解

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)
        }
        
    }

}

你可能感兴趣的:(应用程序跳转详解)