Fetching and displaying managed apps
Provide a consistent app presentation when displaying managed apps.
Overview
Your app can take advantage of Managed App Distribution features to display download status and launch apps. Obtain a list of ManagedApp objects, then display that list of apps in a custom view.
Fetching
This code snippet defines a model that obtains a list of apps from a ManagedAppLibrary.
import ManagedAppDistribution
import Observation
// Define a model that obtains a list of managed apps.
@Observable final class ViewModel {
var content: [Content] = []
enum Content {
case managedApp(ManagedApp), developerContent(title: String, action: () -> Void)
}
func getApps() async {
do {
for try await result in ManagedAppLibrary.currentDistributor.availableApps {
content = try result.get().map(Content.managedApp)
}
} catch {
// Handle errors here.
}
}
}Displaying
After you fetch the list of apps from the model, display them in a compact content style within your custom view.
[Image]
This code snippet demonstrates how to display the list of apps.
import ManagedAppDistribution
import SwiftUI
struct AppList: View {
private var viewModel = ViewModel()
var body: some View {
List(viewModel.content) { content in
switch content {
case let .managedApp(managedApp):
ManagedAppView(app: managedApp)
case let .developerContent(title, action):
ManagedContentView(primaryText: title, offerState: .custom("Request"), offerAction: action) {
Image(name: "custom")
}
}
}
.managedContentStyle(.compact)
.task { await viewModel.getApps() }
}
}