1.跳轉(zhuǎn)到另一個程序的主界面
-
每個程序都該有一個對應(yīng)的Scheme,以確定對應(yīng)的url
Snip20160816_2.png -
一個程序要跳轉(zhuǎn)到(打開)另外一個程序,需要將另外一個程序的Scheme添加到自己的應(yīng)用程序白名單中(在info.plist中配置:LSApplicationQueriesSchemes,類型為數(shù)組,在數(shù)組中添加相應(yīng)的Scheme)->ios9.0開始
Snip20160816_3.png - 跳轉(zhuǎn)代碼
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.跳轉(zhuǎn)到另一個程序的指定界面
- 完成上面程序間跳轉(zhuǎn)的相應(yīng)設(shè)置
- 實現(xiàn)跳轉(zhuǎn)代碼(與跳轉(zhuǎn)到主頁相比,url多了參數(shù),?前面參數(shù)是目標(biāo)程序想要跳轉(zhuǎn)界面的segu標(biāo)簽,?后面是當(dāng)前程序的scheme)
// MARK: - 跳轉(zhuǎn)微信朋友圈
@IBAction func jumpToWeChatTimeLine(sender: AnyObject) {
openURL("WeChat://TimeLine?xinWen")
}
// MARK: - 跳轉(zhuǎn)微信好友
@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)
}
- 在目標(biāo)程序AppDelegate中監(jiān)聽用來跳轉(zhuǎn)的相應(yīng)信息,根據(jù)這些信息讓目標(biāo)程序自己實現(xiàn)頁面切換
extension AppDelegate {
//監(jiān)聽當(dāng)前程序被其他程序通過什么樣的Url打開
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
//根據(jù)url跳轉(zhuǎn)對應(yīng)頁面
//1.url轉(zhuǎn)化成字符串
let urlString = url.absoluteString
//2.獲取首頁控制器
let rootVc = application.keyWindow?.rootViewController
let mainVc = rootVc?.childViewControllers[0] as! ViewController
//將url傳遞給mianVc
mainVc.urlString = urlString
//3.根據(jù)字符串內(nèi)容完成對應(yīng)跳轉(zhuǎn)
if urlString.containsString("Session") {//跳轉(zhuǎn)好友
mainVc.performSegueWithIdentifier("Session", sender: nil)
}else if urlString.containsString("TimeLine") {//跳轉(zhuǎn)朋友圈
mainVc.performSegueWithIdentifier("TimeLine", sender: nil)
}
return true
}
}
3.如何從目標(biāo)程序的非主頁界面回到當(dāng)前(跳轉(zhuǎn)前)程序呢?
思路: 只要在目標(biāo)程序的非主頁界面知道跳轉(zhuǎn)前的程序的URL即可直接跳轉(zhuǎn),所以,這里的關(guān)鍵是如何將跳轉(zhuǎn)前的程序的URL傳遞到目標(biāo)程序的非主頁界面.
- 在目標(biāo)控制器APPDelegate中能獲取到用來跳轉(zhuǎn)的URl信息的方法中將url傳遞給mianVC(事先定義好接收數(shù)據(jù)的屬性),如上面代碼所示.
- 在mianVc 中將url傳遞給需要切換的控制器(事先定義好接收數(shù)據(jù)的屬性)
//切換界面,需要來到該方法.能夠拿到切換前后的控制器
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Session" {
let sessionVc = segue.destinationViewController as! SessionViewController
//傳遞數(shù)據(jù)
sessionVc.urlString = urlString
}
}
}
- 在目標(biāo)控制器中根據(jù)url信息,獲取跳轉(zhuǎn)前控制器的scheme,從而得到跳轉(zhuǎn)回去的url.
class SessionViewController: UIViewController {
//接收數(shù)據(jù)
var urlString = ""
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "退回跳前應(yīng)用", style: .Plain, target: self, action: #selector(backToStartApp))
}
}
extension SessionViewController {
func backToStartApp() {
//分割Url,獲取跳轉(zhuǎn)前的程序的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)
}
}
}