在 Xcode 12 上,此代码完美运行。你可以想象这也是一个常规的 URL。在源应用程序中,您可以使用参数调用和打开目标 URL,例如
let url = URL(string: "DestinationApp:PATH?PARAMETER=11111")
UIApplication.shared.open(url!) { (result) in
if result {
print(result)
// The URL was delivered successfully!
}
}
目标应用程序可以使用此方法处理 AppDelegate 中的方法。警报用于双重检查。
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
// Determine who sent the URL.
let sendingAppID = options[.sourceApplication]
print("source application = \(sendingAppID ?? "Unknown")")
// Process the URL.
guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
let path = components.path,
let params = components.queryItems else {
print("Invalid URL or path missing")
return false
}
if let parameter = params.first(where: { $0.name == "PARAMETER" })?.value {
print("path = \(path)")
print("parameter = \(parameter)")
let alert = UIAlertController(title: "Path = \(path)", message: "Parameter = \(parameter)", preferredStyle: .alert)
let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
if var topController = keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(alert, animated: true, completion: nil)
}
return true
} else {
print("parameter is missing")
return false
}
}