DeepSeek R1
Réfléchi pendant 0 secondes
Processus de raisonnement
<think> Okay, the user wants an Xcode program to check available storage on an Apple TV. Let me think about how to approach this.
First, I remember that Apple TV uses tvOS, so the app needs to be built for that platform. The code should be in Swift, probably using SwiftUI since that's the modern framework.
I need to access the file system to get storage information. On iOS, we use URLResourceKey.volumeAvailableCapacityForImportantUsage, but does tvOS support that? I should check. Assuming it's available, that's the way to go.
Wait, Apple TV's storage might be similar to iOS. So using FileManager.default.urls(for: .documentDirectory, ...) might not be the right path. Maybe the volume information is for the entire device, not just the app's container. So using the document directory's URL might still work as a reference point.
Next steps: Create a SwiftUI view that displays the available space. The view should show GB, so I need to convert bytes to gigabytes. Formatting the number to two decimal places would be good.
Also, handle errors. If retrieving the values fails, show an error message. Use do-try-catch blocks when getting the resource values.
Testing on Apple TV: The user needs to set the deployment target to tvOS, run it on a simulator or real device. But wait, does the simulator provide accurate storage data? Maybe not, but it's a start.
Potential issues: Permissions. Do tvOS apps have access to storage info without special entitlements? I think so, since it's the same as iOS for non-sensitive data. But maybe on tvOS, the available capacity is reported differently.
Putting it all together: The ContentView will have a VStack showing the available space. The model will fetch the data using FileManager. Handle the conversion from bytes to GB. Display error if something goes wrong.
I should also mention that this code is a starting point and might need adjustments based on tvOS specifics. Maybe include a note about testing on a real device if possible.
</think>
Voici un exemple de programme SwiftUI pour tvOS qui calcule l'espace disponible sur un Apple TV :
Copier
import SwiftUI
struct ContentView: View {
@State private var availableSpace: String = ""
@State private var errorMessage: String = ""
var body: some View {
VStack(spacing: 20) {
Image(systemName: "appletv")
.font(.system(size: 60))
Text("Espace disponible")
.font(.title)
if !availableSpace.isEmpty {
Text(availableSpace)
.font(.largeTitle)
.foregroundColor(.green)
}
if !errorMessage.isEmpty {
Text(errorMessage)
.foregroundColor(.red)
}
}
.onAppear(perform: checkStorage)
}
func checkStorage() {
do {
let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory())
let freeSpace = (systemAttributes[.systemFreeSize] as? NSNumber)?.doubleValue ?? 0
// Conversion en Go
let freeGB = freeSpace / 1_000_000_000
availableSpace = String(format: "%.2f Go", freeGB)
} catch {
errorMessage = "Erreur de lecture : \(error.localizedDescription)"
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Pour utiliser ce programme :
Créez un nouveau projet dans Xcode en choisissant "tvOS App"
Sélectionnez SwiftUI comme interface
Remplacez le contenu de ContentView.swift par ce code
Exécutez sur le simulateur Apple TV ou sur un appareil physique
Ce programme :
Affiche une interface simple avec l'icône Apple TV
Calcule l'espace libre du système de fichiers
Convertit le résultat en Gigaoctets
Affiche le résultat avec 2 décimales
Gère les erreurs éventuelles
Notez que :
Les informations de stockage peuvent varier selon la version de tvOS
Les résultats peuvent différer légèrement entre simulateur et appareil réel
L'accès au stockage est limité à l'espace système disponible (pas d'accès aux autres apps)