swift - 从自定义框架显示 UIAlertViewController
问题描述
我有一个自定义框架(.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)
}
尽管类似问题提供了一些技巧,但我已经在这方面工作了几天,但未能取得任何进展。
非常感谢
解决方案
推荐阅读
- laravel - 如何在 Laravel 中将图像存储在两个不同的文件夹中
- ios - 复选标记未显示在表格单元格中
- python - 安装 python httpclient 时遇到错误
- python-3.x - 如何使用 sqlalchemy 插入选择字段?
- javascript - 如何在我的 chrome 扩展程序和我的 Node 服务器之间启用跨域 AJAX 请求?
- excel - 根据范围内的唯一值在多个工作簿中拆分工作表
- c - 处理 C 中的数学错误
- php - php echo返回jQuery失败
- ruby-on-rails - 如何通过rails控制台上的模型获取通讯员ID
- php - 返回页面时显示已经存在的“财富”