DebugActions
DebugActions
是一个框架,它抽象化了iOS 13的UIContextMenuInteraction
API,以增强通过强触可访问的仅用于调试的自定义操作的自定义UIView
。您可以使用此框架为视图添加有用的辅助操作,以增强您的日常调试流程,例如显示生成的视图的底层数据或将某些内容复制到粘贴板。
用法
DebugAction
的API对所有UIViews
都可用。它首先定义一个
DebugMenuInteractionDelegate
(可以是自己视图),它会返回一个包含所有可用操作的UIMenuElement
,形式为UIKit
extension LoggedUserView: DebugMenuInteractionDelegate {
func copyUUID() -> UIMenuElement {
return UIAction(title: "Copy UUID") { _ in
// Copy the UUID to pasteboard
}
}
func printLoginJSON() -> UIMenuElement {
return UIAction(title: "Print the Backend's Login Response") { _ in
// Print it!
}
}
func debugActions() -> [UIMenuElement] {
#if DEBUG // Make sure to provide them only in developer builds!
return [copyUUID(), printLoginJSON()]
#else
return []
#endif
}
}
现在,您只需在视图中启用调试操作!
override func viewDidLoad() {
super.viewDidLoad()
loggedUserView.enableDebugActionsInteraction()
}
异步操作
如果您支持
iOS 14
,您还可以使用新的UIDeferredMenuElement
来创建异步解析的操作
func serverInformation() -> UIMenuElement {
return UIDeferredMenuElement { completion in
// an Async task that fetches some information about a server:
completion([printServerInformationAction(serverInfo)])
}
}
这会导致一个带加载指示器的菜单项,一旦完成处理程序被调用,它会替换成最终的操作。
安装
Swift 包管理器
.package(url: "https://github.com/rockbruno/DebugActions", .upToNextMinor(from: "0.2.0"))
CocoaPods
pod 'DebugActions'