应用间跳转

先来回顾一下App是如何操作打电话,发短信,网址跳转的

/**
    打电话:tel://
    发短信:sms://
    访问网址:http://
**/
let url = URL(string: "sms://10000")
if #available(iOS 10.0, *) {
    //关于iOS10.0之后的方法请看iOS10被弃用的openURL
    UIApplication.shared.open(url!, options: [UIApplicationOpenURLOptionUniversalLinksOnly : true], completionHandler: nil)
} else {
    // Fallback on earlier versions
    UIApplication.shared.openURL(url!)
}

关于Scheme

先看看关于URL的几个属性

let url = URL(string: "https://www.appannie.com/account/login/?_ref=header")
print("scheme:\(url?.scheme) host:\(url?.host) path:\(url?.path) query:\(url?.query)")

//输出:scheme:Optional("https") host:Optional("www.appannie.com") path:Optional("/account/login") query:Optional("_ref=header")

跳转

先创建一个被跳转的App

在App做如下设置

下面创建跳转的App

iOS9.0之后,info.plist需要加入如下字段

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>abc</string>
    <!--需要跳转多少的scheme,就添加多少该字段-->
</array>

然后执行如下方法即可

func openAnotherApp() {

    //1.定义要跳转的App的Scheme
    let url = URL(string: "abc://")

    //2.打开定义好的Scheme
    if UIApplication.shared.canOpenURL(url!) {
        UIApplication.shared.openURL(url!)
    } else {
        print("该App未安装")
    }
}

跳转应用并切换界面

假如A应用要跳转到B应用的某个界面,只需在要跳转的URL添加一些标识字段,比如 abc://xxx,xxx就可以看做是标识

A

func openAnotherApp(_ host:String = "") {

    //1.定义要跳转的App的Scheme
    let url = URL(string: "abc://\(host)")

    //2.打开定义好的Scheme
    if UIApplication.shared.canOpenURL(url!) {
        UIApplication.shared.openURL(url!)
    } else {
        print("该App未安装")
    }
}

@IBAction func gotoFirst(_ sender: UIButton) {

    openAnotherApp("first")
}

@IBAction func gotoSecond(_ sender: UIButton) {

    openAnotherApp("second")
}

然后在B的AppDelegate中实现

@available(iOS 9.0,*)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    guard let host = url.host else { return true }

    jump(host: host)

    return true
}

@available(iOS 2.0,*)
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {

    guard let host = url.host else { return true }

    jump(host: host)

    return true
}

func jump(host:String) {

    let navigationController = window?.rootViewController as! UINavigationController

    //为了防止B应用处于打开状态,并跳转到某个界面,要提前将所有push进来的其他ViewController移除
    navigationController.popToRootViewController(animated: false)
    let viewController = navigationController.childViewControllers.first!

    print(host)
    switch host {
    case "first":
        viewController.performSegue(withIdentifier: "first", sender: nil)

    case "second":
        viewController.performSegue(withIdentifier: "second", sender: nil)

    default:
        break
    }
}

捕捉到url的host,根据自己定义的标识来打开不同的界面即可,比如Segue方式。这样就可以实现跳转到某界面

9.0之前的跳转回原来的App

func jumpBack() {

    // 获取需要跳转回去的scheme
    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    guard let backScheme = delegate.backScheme else {return}

    let url = NSURL(string: backScheme + "://")
    if UIApplication.sharedApplication().canOpenURL(url!) {
        UIApplication.sharedApplication().openURL(url!)
    }

}

常用设置界面跳转

  1. 设置scheme

  2. 常用跳转URL

About — prefs:root=General&path=About
Accessibility — prefs:root=General&path=ACCESSIBILITY
AirplaneModeOn— prefs:root=AIRPLANE_MODE
Auto-Lock — prefs:root=General&path=AUTOLOCK
Brightness — prefs:root=Brightness
Bluetooth — prefs:root=General&path=Bluetooth
Date& Time — prefs:root=General&path=DATE_AND_TIME
FaceTime — prefs:root=FACETIME
General— prefs:root=General
Keyboard — prefs:root=General&path=Keyboard
iCloud — prefs:root=CASTLE  iCloud
Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP
International — prefs:root=General&path=INTERNATIONAL
Location Services — prefs:root=LOCATION_SERVICES                          
Wi-Fi — prefs:root=WIFISetting—prefs:root=INTERNET_TETHERING
Wallpaper — prefs:root=Wallpaper
VPN — prefs:root=General&path=Network/VPN
Twitter — prefs:root=TWITTER  Usage — prefs:root=General&path=USAGE 
Store — prefs:root=STORE
SoftwareUpdate— prefs:root=General&path=SOFTWARE_UPDATE_LINK 
Sounds — prefs:root=Sounds
Siri — prefs:root=General&path=Assistant 
Safari — prefs:root=Safari 
Reset — prefs:root=General&path=Reset 
Profile — prefs:root=General&path=ManagedConfigurationList 
Phone — prefs:root=Phone  Photos — prefs:root=Photos
Notification — prefs:root=NOTIFICATIONS_ID
Notes — prefs:root=NOTES
Nike + iPod — prefs:root=NIKE_PLUS_IPOD 
Network — prefs:root=General&path=Network 
Music VolumeLimit— prefs:root=MUSIC&path=VolumeLimit 
Music Equalizer — prefs:root=MUSIC&path=EQ 
Music — prefs:root=MUSIC

results matching ""

    No results matching ""