源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS应用程序之间的几种跳转情况详解

  • 时间:2021-12-14 08:20 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS应用程序之间的几种跳转情况详解
[b]前言[/b] 在iOS开发的过程中,我们经常会遇到比如需要从一个应用程序A跳转到另一个应用程序B的场景。这就需要我们掌握iOS应用程序之间的相互跳转知识。下面我们就常用到的几种跳转情况进行介绍。 [b]一、跳转到另一个程序的主界面[/b] 每个程序都该有一个对应的Scheme,以确定对应的url [img]http://files.jb51.net/file_images/article/201609/2016928115217077.png?2016828115226[/img] 一个程序要跳转到(打开)另外一个程序,需要将另外一个程序的Scheme添加到自己的应用程序白名单中(在info.plist中配置:LSApplicationQueriesSchemes,类型为数组,在数组中添加相应的Scheme)->ios9.0开始 [img]http://files.jb51.net/file_images/article/201609/2016928115312686.png?2016828115322[/img] [b]跳转代码[/b]
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)
  }

 }
}
[b]二、跳转到另一个程序的指定界面[/b] 完成上面程序间跳转的相应设置 实现跳转代码(与跳转到主页相比,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
 }
}
[b]三、如何从目标程序的非主页界面回到当前(跳转前)程序呢?[/b] [b]思路: [/b]只要在目标程序的非主页界面知道跳转前的程序的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)
  }

 }

}
[b]总结[/b] 以上就是关于iOS应用程序之间跳转的全部内容,希望能对各位iOS开发者们有所帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部