前言
在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開發人員們有所協助,如果有疑問大家可以留言交流。