首页 > 解决方案 > 打开 UIDocument 时如何显示错误消息?

问题描述

我有一个基于文档的 iOS 应用程序,当它第一次打开文档时,主视图控制器调用UIDocument.open.

document.open { success in
    if success { ... set up UI ... }
    else { ??? }
}

这里的问题是如果success为假,我无权访问该错误。通常,Apple 的 API 会Error在这些情况下将可选参数传递给回调,但出于某种原因,它们不会在这里。

我发现这个方法可以在我的应用程序的子类中重写UIDocument

override func handleError(_ error: Error, userInteractionPermitted: Bool) {

现在在那个方法中,我有,Error但我无法轻松访问调用的视图控制器,document.open我需要呈现类似 a 的内容UIAlertController来显示错误消息。此handleError方法也在非主线程上调用。

看起来我需要通过在实例或全局变量中传递信息来进行协调。因为这似乎比 Apple 的通常设计更尴尬——我希望 Error 在 的完成处理程序中可用open,我想我可能会遗漏一些东西。

是否有另一种推荐的方法来获取错误对象并向用户显示消息?

标签: ioscocoa-touchios11uidocument

解决方案


抢,

如果你真的想“迅速”,你可以实现一个闭包来完全做到这一点,而不需要静态/全局变量。

我将首先定义一个枚举来模拟对 UIDocument 的 API 调用的成功和失败案例。通用的 Result 枚举是一种非常常见的方法。

enum Result<T> {
    case failure(Error)
    case success(T)
}

从那里我会在你的类中定义一个可选的闭包来处理 UIDocument.open 的结果

我要做的实现是这样的:

class DocumentManager: UIDocument {

    var onAttemptedDocumentOpen: ((Result<Bool>) -> Void)?

    func open(document: UIDocument){
        document.open { result in
            guard result else { return } // We only continue if the result is successful

            // Check to make sure someone has set a function that will handle the outcome
            if let onAttemptedDocumentOpen = self.onAttemptedDocumentOpen {
                onAttemptedDocumentOpen(.success(result))
            }
        }
    }

    override func handleError(_ error: Error, userInteractionPermitted: Bool) {
        // Check to make sure someone has set a function that will handle the outcome
        if let onAttemptedDocumentOpen = self.onAttemptedDocumentOpen {
            onAttemptedDocumentOpen(.failure(error))
        }
    }

}

然后我来自任何类将使用 DocumentManager 你会做这样的事情:

class SomeOtherClassThatUsesDocumentManager {

    let documentManger = DocumentManager()

    let someViewController = UIViewController()

    func someFunction(){
        documentManger.onAttemptedDocumentOpen = { (result) in
            switch result {
            case .failure(let error):
                DispatchQueue.main.async {
                    showAlert(target: self.someViewController, title: error.localizedDescription)
                }

            case .success(_):
                // Do something
                return
            }
        }
    }
}

奖励:这是我编写的一个静态函数,用于在某些视图控制器上显示 UIAlertController

/** Easily Create, Customize, and Present an UIAlertController on a UIViewController

 - Parameters:
    - target: The instance of a UIViewController that you would like to present tye UIAlertController upon.
    - title: The `title` for the UIAlertController.
    - message: Optional `message` field for the UIAlertController. nil by default
    - style: The `preferredStyle` for the UIAlertController. UIAlertControllerStyle.alert by default
    - actionList: A list of `UIAlertAction`. If no action is added, `[UIAlertAction(title: "OK", style: .default, handler: nil)]` will be added.

 */
func showAlert(target: UIViewController, title: String, message: String? = nil, style: UIAlertControllerStyle = .alert, actionList: [UIAlertAction] = [UIAlertAction(title: "OK", style: .default, handler: nil)] ) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: style)
    for action in actionList {
        alert.addAction(action)
    }
    // Check to see if the target viewController current is currently presenting a ViewController
    if target.presentedViewController == nil {
        target.present(alert, animated: true, completion: nil)
    }
}

推荐阅读