iOS應用程式之間的幾種跳轉情況詳解_IOS

來源:互聯網
上載者:User

前言

在iOS開發的過程中,我們經常會遇到比如需要從一個應用程式A跳轉到另一個應用程式B的情境。這就需要我們掌握iOS應用程式之間的相互跳轉知識。下面我們就常用到的幾種跳轉情況進行介紹。

一、跳轉到另一個程式的主介面

每個程式都該有一個對應的Scheme,以確定對應的url


一個程式要跳轉到(開啟)另外一個程式,需要將另外一個程式的Scheme添加到自己的應用程式白名單中(在info.plist中配置:LSApplicationQueriesSchemes,類型為數組,在數組中添加相應的Scheme)->ios9.0開始


跳轉代碼

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

二、跳轉到另一個程式的指定介面

完成上面程式間跳轉的相應設定

實現跳轉代碼(與跳轉到首頁相比,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 }}

三、如何從目標程式的非首頁介面回到當前(跳轉前)程式呢?

思路: 只要在目標程式的非首頁介面知道跳轉前的程式的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)  } }}

總結

以上就是關於iOS應用程式之間跳轉的全部內容,希望能對各位iOS開發人員們有所協助,如果有疑問大家可以留言交流。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.