metatext/Extensions/UIVIewController+Extensions.swift

45 lines
1.4 KiB
Swift
Raw Permalink Normal View History

2020-12-16 01:39:38 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-03-03 06:55:35 +00:00
import SafariServices
2020-12-16 01:39:38 +00:00
import UIKit
import ViewModels
extension UIViewController {
2021-03-09 04:40:22 +00:00
var isVisible: Bool { isViewLoaded && view.window != nil }
2021-03-06 05:48:11 +00:00
2020-12-16 01:39:38 +00:00
func present(alertItem: AlertItem) {
let alertController = UIAlertController(
title: nil,
message: alertItem.error.localizedDescription,
preferredStyle: .alert)
let okAction = UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .default) { _ in }
alertController.addAction(okAction)
present(alertController, animated: true)
}
2021-03-03 06:55:35 +00:00
#if !IS_SHARE_EXTENSION
func open(url: URL, identityContext: IdentityContext) {
func openWithRegardToBrowserSetting(url: URL) {
if identityContext.appPreferences.openLinksInDefaultBrowser || !url.isHTTPURL {
UIApplication.shared.open(url)
} else {
present(SFSafariViewController(url: url), animated: true)
}
}
if identityContext.appPreferences.useUniversalLinks {
UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { success in
if !success {
openWithRegardToBrowserSetting(url: url)
}
}
} else {
openWithRegardToBrowserSetting(url: url)
}
}
#endif
2020-12-16 01:39:38 +00:00
}