我有一个自定义框架(.xcframework),我希望在其中一个函数UIAlertViewController
中出现 a ,如果用户单击Continue,则继续执行该函数,否则如果点击 Cancel则从该函数返回。有UIAlertViewController
时间限制,所以如果用户不与对话框交互,函数会自动返回。
这是我框架中的函数以及extension UIAlertViewController
:
extension UIAlertController {
func presentInOwnWindow(animated: Bool, completion: (() -> Void)?) {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIViewController()
window.windowLevel = .alert
window.makeKeyAndVisible()
window.rootViewController?.present(self, animated: animated, completion: completion)
}
}
public enum ApiClientError: Error {
case timeout
case userCancelled
}
public enum ApiClient {
public static func get(showAlert: Bool = true) throws -> String {
if showAlert {
var didContinue = false
let semaphore = DispatchSemaphore(value: 0)
let alert = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Continue", style: .default, handler: { action in
print("continue")
didContinue = true
semaphore.signal()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
print("cancel")
semaphore.signal()
}))
DispatchQueue.main.async {
alert.presentInOwnWindow(animated: true, completion: nil)
}
if semaphore.wait(timeout: .now() + DispatchTimeInterval.seconds(10)) == .timedOut {
alert.dismiss(animated: true)
throw ApiClientError.timeout
}
if didContinue {
return "Continue after alert"
}
else {
throw ApiClientError.userCancelled
}
}
else {
return "Continue without alert"
}
}
}
在引用框架的调用应用程序中,警报根本没有出现,我收到超时错误。
do {
let result = ApiClient.get()
print(result)
}
catch let error {
print(error.localizedDescription)
}
尽管类似问题提供了一些技巧,但我已经在这方面工作了几天,但未能取得任何进展。
非常感谢