diff --git a/.github/workflows/example.yml b/.github/workflows/example.yml new file mode 100644 index 0000000..d9792fb --- /dev/null +++ b/.github/workflows/example.yml @@ -0,0 +1,27 @@ +name: Example app + +on: + push: + branches: [master] + pull_request: + +permissions: + contents: read + +jobs: + build: + name: Build SoundBrowser (iOS Simulator) + runs-on: macos-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Show toolchain + run: xcodebuild -version + + - name: Build + run: | + xcodebuild build \ + -project Examples/SoundBrowser/SoundBrowser.xcodeproj \ + -scheme SoundBrowser \ + -destination 'generic/platform=iOS Simulator' \ + CODE_SIGNING_ALLOWED=NO diff --git a/Examples/SoundBrowser/README.md b/Examples/SoundBrowser/README.md new file mode 100644 index 0000000..1f01358 --- /dev/null +++ b/Examples/SoundBrowser/README.md @@ -0,0 +1,17 @@ +# SoundBrowser + +A small SwiftUI app that uses the `SystemSoundsLibrary` Swift package from this repository. + +- Search by name, ID, file name or category +- Sounds grouped by folder (`UISounds`, `UISounds/New`, `UISounds/Modern`, `UISounds/nano`) +- Tap a row to play the sound; the info button shows ID, path, category, availability and confidence +- Share or save the sound file from the details screen + +Open `SoundBrowser.xcodeproj` in Xcode 16 or later and run it on a device with iOS 17 or later. +The iOS Simulator doesn't play system sounds, and the sound files can't be read there, so +sharing is only available on a device. + +The app reads files from `/System/Library/Audio/UISounds` to share them. That's fine for a +developer tool, but don't ship it in an App Store app (App Review Guideline 2.5.2). + +The original Objective-C demo app is still at the repository root (`SystemSoundLibrary.xcodeproj`). diff --git a/Examples/SoundBrowser/SoundBrowser.xcodeproj/project.pbxproj b/Examples/SoundBrowser/SoundBrowser.xcodeproj/project.pbxproj new file mode 100644 index 0000000..b4580c1 --- /dev/null +++ b/Examples/SoundBrowser/SoundBrowser.xcodeproj/project.pbxproj @@ -0,0 +1,267 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXBuildFile section */ + 5B0D00012E00000000000001 /* SystemSoundsLibrary in Frameworks */ = {isa = PBXBuildFile; productRef = 5B0D00012E00000000000002 /* SystemSoundsLibrary */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 5B0D00012E00000000000010 /* SoundBrowser.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SoundBrowser.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + 5B0D00012E00000000000011 /* SoundBrowser */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = SoundBrowser; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5B0D00012E00000000000020 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 5B0D00012E00000000000001 /* SystemSoundsLibrary in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 5B0D00012E00000000000030 = { + isa = PBXGroup; + children = ( + 5B0D00012E00000000000011 /* SoundBrowser */, + 5B0D00012E00000000000031 /* Products */, + ); + sourceTree = ""; + }; + 5B0D00012E00000000000031 /* Products */ = { + isa = PBXGroup; + children = ( + 5B0D00012E00000000000010 /* SoundBrowser.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 5B0D00012E00000000000040 /* SoundBrowser */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5B0D00012E00000000000050 /* Build configuration list for PBXNativeTarget "SoundBrowser" */; + buildPhases = ( + 5B0D00012E00000000000021 /* Sources */, + 5B0D00012E00000000000020 /* Frameworks */, + 5B0D00012E00000000000022 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + 5B0D00012E00000000000011 /* SoundBrowser */, + ); + name = SoundBrowser; + packageProductDependencies = ( + 5B0D00012E00000000000002 /* SystemSoundsLibrary */, + ); + productName = SoundBrowser; + productReference = 5B0D00012E00000000000010 /* SoundBrowser.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 5B0D00012E00000000000060 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 2600; + LastUpgradeCheck = 2600; + TargetAttributes = { + 5B0D00012E00000000000040 = { + CreatedOnToolsVersion = 26.0; + }; + }; + }; + buildConfigurationList = 5B0D00012E00000000000051 /* Build configuration list for PBXProject "SoundBrowser" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 5B0D00012E00000000000030; + minimizedProjectReferenceProxies = 1; + packageReferences = ( + 5B0D00012E00000000000003 /* XCLocalSwiftPackageReference "../.." */, + ); + preferredProjectObjectVersion = 77; + productRefGroup = 5B0D00012E00000000000031 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 5B0D00012E00000000000040 /* SoundBrowser */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 5B0D00012E00000000000022 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 5B0D00012E00000000000021 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 5B0D00012E00000000000070 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_OPTIMIZATION_LEVEL = 0; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 5B0D00012E00000000000071 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 5B0D00012E00000000000072 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_CFBundleDisplayName = "Sound Browser"; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.github.tuner88.SoundBrowser; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 6.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 5B0D00012E00000000000073 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_CFBundleDisplayName = "Sound Browser"; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = com.github.tuner88.SoundBrowser; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_VERSION = 6.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 5B0D00012E00000000000050 /* Build configuration list for PBXNativeTarget "SoundBrowser" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5B0D00012E00000000000072 /* Debug */, + 5B0D00012E00000000000073 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 5B0D00012E00000000000051 /* Build configuration list for PBXProject "SoundBrowser" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5B0D00012E00000000000070 /* Debug */, + 5B0D00012E00000000000071 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCLocalSwiftPackageReference section */ + 5B0D00012E00000000000003 /* XCLocalSwiftPackageReference "../.." */ = { + isa = XCLocalSwiftPackageReference; + relativePath = ../..; + }; +/* End XCLocalSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 5B0D00012E00000000000002 /* SystemSoundsLibrary */ = { + isa = XCSwiftPackageProductDependency; + productName = SystemSoundsLibrary; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 5B0D00012E00000000000060 /* Project object */; +} diff --git a/Examples/SoundBrowser/SoundBrowser.xcodeproj/xcshareddata/xcschemes/SoundBrowser.xcscheme b/Examples/SoundBrowser/SoundBrowser.xcodeproj/xcshareddata/xcschemes/SoundBrowser.xcscheme new file mode 100644 index 0000000..c037ead --- /dev/null +++ b/Examples/SoundBrowser/SoundBrowser.xcodeproj/xcshareddata/xcschemes/SoundBrowser.xcscheme @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/SoundBrowser/SoundBrowser/ContentView.swift b/Examples/SoundBrowser/SoundBrowser/ContentView.swift new file mode 100644 index 0000000..3f54cf4 --- /dev/null +++ b/Examples/SoundBrowser/SoundBrowser/ContentView.swift @@ -0,0 +1,116 @@ +import SwiftUI +import SystemSoundsLibrary + +/// Lists all catalog sounds, grouped by folder. Tap a row to play it. +struct ContentView: View { + @State private var query = "" + @State private var hideRemoved = false + @State private var selected: SystemSound? + @State private var playCount = 0 + + var body: some View { + NavigationStack { + List { + ForEach(sections) { section in + Section { + ForEach(section.sounds) { sound in + SoundRow(sound: sound) { + SystemSoundPlayer.play(sound) + playCount += 1 + } showDetails: { + selected = sound + } + } + } header: { + Text(verbatim: "\(section.id) (\(section.sounds.count))") + } + } + } + .overlay { + if sections.isEmpty { + ContentUnavailableView.search(text: query) + } + } + .searchable(text: $query, prompt: "Name, ID, file or category") + .navigationTitle("System Sounds") + .navigationDestination(item: $selected) { sound in + SoundDetailView(sound: sound) + } + .toolbar { + ToolbarItem(placement: .topBarTrailing) { + Menu("Filter", systemImage: "line.3.horizontal.decrease.circle") { + Toggle("Hide removed sounds", isOn: $hideRemoved) + } + } + } + .sensoryFeedback(.selection, trigger: playCount) + } + } + + private var sections: [SoundSection] { + let needle = query.trimmingCharacters(in: .whitespaces).lowercased() + let filtered = SystemSound.all.filter { sound in + if hideRemoved && sound.status == .removed { return false } + guard !needle.isEmpty else { return true } + return [sound.name, String(sound.id), sound.fileName ?? "", sound.category ?? ""] + .contains { $0.lowercased().contains(needle) } + } + let grouped = Dictionary(grouping: filtered) { $0.groupTitle } + return grouped.keys.sorted().map { SoundSection(id: $0, sounds: grouped[$0] ?? []) } + } +} + +struct SoundSection: Identifiable { + let id: String + let sounds: [SystemSound] +} + +struct SoundRow: View { + let sound: SystemSound + let play: () -> Void + let showDetails: () -> Void + + var body: some View { + HStack { + Button(action: play) { + VStack(alignment: .leading, spacing: 2) { + HStack(spacing: 6) { + Text(sound.name) + if sound.status == .removed { + Text("removed") + .font(.caption2) + .foregroundStyle(.secondary) + } + } + Text(verbatim: "\(sound.id) · \(sound.path ?? "no file")") + .font(.caption.monospaced()) + .foregroundStyle(.secondary) + .lineLimit(1) + .truncationMode(.middle) + } + .frame(maxWidth: .infinity, alignment: .leading) + .contentShape(Rectangle()) + } + .buttonStyle(.plain) + .accessibilityHint("Plays the sound") + + Button("Details", systemImage: "info.circle", action: showDetails) + .labelStyle(.iconOnly) + .buttonStyle(.borderless) + } + } +} + +extension SystemSound { + var groupTitle: String { + switch folder { + case nil: "No file" + case ""?: "UISounds" + case let folder?: "UISounds/\(folder)" + } + } +} + +#Preview { + ContentView() +} diff --git a/Examples/SoundBrowser/SoundBrowser/SoundBrowserApp.swift b/Examples/SoundBrowser/SoundBrowser/SoundBrowserApp.swift new file mode 100644 index 0000000..50baffa --- /dev/null +++ b/Examples/SoundBrowser/SoundBrowser/SoundBrowserApp.swift @@ -0,0 +1,10 @@ +import SwiftUI + +@main +struct SoundBrowserApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} diff --git a/Examples/SoundBrowser/SoundBrowser/SoundDetailView.swift b/Examples/SoundBrowser/SoundBrowser/SoundDetailView.swift new file mode 100644 index 0000000..4b0b821 --- /dev/null +++ b/Examples/SoundBrowser/SoundBrowser/SoundDetailView.swift @@ -0,0 +1,86 @@ +import SwiftUI +import SystemSoundsLibrary + +/// Shows everything known about a sound and lets you share or save its file. +struct SoundDetailView: View { + let sound: SystemSound + @State private var exportURL: URL? + + var body: some View { + Form { + Section { + Button("Play", systemImage: "play.fill") { + SystemSoundPlayer.play(sound) + } + Button("Play as alert", systemImage: "bell.fill") { + SystemSoundPlayer.playAlert(sound) + } + if let exportURL { + ShareLink(item: exportURL) { + Label("Share or save file", systemImage: "square.and.arrow.up") + } + } + } footer: { + if sound.fileURL != nil && exportURL == nil { + Text("The sound file can't be read here (for example in the Simulator).") + } + } + + Section("Details") { + LabeledContent("ID", value: String(sound.id)) + LabeledContent("Name", value: sound.name) + LabeledContent("Path") { + Text(sound.path ?? "—") + .font(.caption.monospaced()) + .textSelection(.enabled) + } + LabeledContent("Category", value: sound.category ?? "—") + LabeledContent( + "Available since", + value: sound.minimumIOSVersion.map { "iOS \($0)" } ?? "Before iOS 16 or unknown" + ) + LabeledContent("Status", value: sound.status.label) + LabeledContent("Confidence", value: sound.confidence.rawValue) + } + } + .navigationTitle(sound.name) + .navigationBarTitleDisplayMode(.inline) + .task(id: sound) { + exportURL = Self.exportCopy(of: sound) + } + } + + /// Copies the sound file into the temporary directory so it can be shared. + private static func exportCopy(of sound: SystemSound) -> URL? { + guard let source = sound.fileURL, let fileName = sound.fileName, + FileManager.default.isReadableFile(atPath: source.path) else { return nil } + let directory = FileManager.default.temporaryDirectory.appendingPathComponent("Export", isDirectory: true) + let destination = directory.appendingPathComponent(fileName) + do { + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + if FileManager.default.fileExists(atPath: destination.path) { + try FileManager.default.removeItem(at: destination) + } + try FileManager.default.copyItem(at: source, to: destination) + return destination + } catch { + return nil + } + } +} + +extension SystemSound.Status { + var label: String { + switch self { + case .current: "Current" + case .changedSinceIOS7: "Changed since iOS 7" + case .removed: "Removed" + } + } +} + +#Preview { + NavigationStack { + SoundDetailView(sound: .mailReceived) + } +}