Files
iOS/Sources/App/Utilities/Utils.swift
Bruno Pantaleão Gonçalves 8c46161ff0 Replace usage of UIApplication.shared by URLOpener class (#3961)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 15:41:49 +01:00

77 lines
2.8 KiB
Swift

import Foundation
import KeychainAccess
import RealmSwift
import SafariServices
import Shared
import Version
func resetStores() {
do {
try keychain.removeAll()
} catch {
Current.Log.error("Error when trying to delete everything from Keychain!")
}
let bundleId = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: bundleId)
UserDefaults.standard.removePersistentDomain(forName: AppConstants.AppGroupID)
Realm.reset()
}
func openURLInBrowser(_ urlToOpen: URL, _ sender: UIViewController?) {
guard ["http", "https"].contains(urlToOpen.scheme?.lowercased()) else {
URLOpener.shared.open(urlToOpen, options: [:], completionHandler: nil)
return
}
let browserPreference = prefs.string(forKey: "openInBrowser")
.flatMap { OpenInBrowser(rawValue: $0) } ?? .Safari
let privateTabPreference = prefs.bool(forKey: "openInPrivateTab")
switch browserPreference {
case .Chrome where OpenInChromeController.sharedInstance.isChromeInstalled():
OpenInChromeController.sharedInstance.openInChrome(urlToOpen, callbackURL: nil)
case .Firefox where OpenInFirefoxControllerSwift().isFirefoxInstalled():
OpenInFirefoxControllerSwift().openInFirefox(urlToOpen, privateTab: privateTabPreference)
case .FirefoxFocus where OpenInFirefoxControllerSwift(type: .focus).isFirefoxInstalled():
OpenInFirefoxControllerSwift(type: .focus).openInFirefox(urlToOpen)
case .FirefoxKlar where OpenInFirefoxControllerSwift(type: .klar).isFirefoxInstalled():
OpenInFirefoxControllerSwift(type: .klar).openInFirefox(urlToOpen)
case .SafariInApp where sender != nil:
let sfv = SFSafariViewController(url: urlToOpen)
sender!.present(sfv, animated: true)
default:
URLOpener.shared.open(urlToOpen, options: [:], completionHandler: nil)
}
}
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data) as? [String: Any]
} catch {
Current.Log.error("Error converting JSON string to dict: \(error)")
}
}
return nil
}
func setDefaults() {
prefs.set(AppConstants.build, forKey: "lastInstalledBundleVersion")
prefs.set(AppConstants.version, forKey: "lastInstalledShortVersion")
if prefs.object(forKey: "openInBrowser") == nil {
if prefs.bool(forKey: "openInChrome") {
prefs.set(OpenInBrowser.Chrome.rawValue, forKey: "openInBrowser")
prefs.removeObject(forKey: "openInChrome")
} else {
prefs.set(OpenInBrowser.Safari.rawValue, forKey: "openInBrowser")
}
}
if prefs.object(forKey: "confirmBeforeOpeningUrl") == nil {
prefs.setValue(true, forKey: "confirmBeforeOpeningUrl")
}
}