如何在iOS应用中通过代码打开项目内的Shortcut文件?
解决iOS应用中打开Shortcut文件的问题
1. 代码修正
你的代码错误地将URL转为字符串传入open方法,UIApplication.shared.open需要接收URL类型参数,而非字符串。修正后的代码如下:
if let url = Bundle.main.url(forResource: "saverone", withExtension: "shortcut") { UIApplication.shared.open(url) { success in print("Shortcut opened: \(success)") } }
2. Info.plist配置简化
LSApplicationQueriesSchemes只需添加合法的URL Scheme,不需要具体文件路径或完整URL。Shortcuts应用的核心Scheme是shortcuts,修正后的配置如下:
<key>LSApplicationQueriesSchemes</key> <array> <string>shortcuts</string> </array>
3. 更可靠的快捷指令导入方式
如果目标是将快捷指令导入Shortcuts应用,建议使用Shortcuts官方提供的导入Scheme,比直接打开文件更稳定:
if let localUrl = Bundle.main.url(forResource: "saverone", withExtension: "shortcut"), let encodedUrl = localUrl.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) { let importUrl = URL(string: "shortcuts://import-shortcut?url=\(encodedUrl)")! UIApplication.shared.open(importUrl) { success in print("Shortcut imported: \(success)") } }
额外检查项
- 确认
saverone.shortcut已添加到Xcode项目,并在Target Membership中勾选当前应用目标,确保文件被打包进App Bundle。 - 设备需安装Shortcuts应用,否则无法打开或导入快捷指令。
内容的提问来源于stack exchange,提问作者Roei Nadam
相关产品推荐
相关产品推荐

