ios – How to stop the sound in silent mode


I have a counter that plays a “tick” sound everytime its tapped. I tried the app on my physical device and the sound plays even when the phone is in silent mode. How do I make it stop playing the sound when the silent mode is on?

import AVFoundation

struct View1 : View {
    @State var player : AVAudioPlayer?

    var body: some View {
           Button(action: {
                      todoItem.count += 1

                   }) {
                         .frame(width: 70, height: 70)

func playSound() {

        guard let url = Bundle.main.url(forResource: "tick", withExtension: "mp3") else { return }

        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
            try AVAudioSession.sharedInstance().setActive(true)

            /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
            player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

            guard let player = player else { return }

        } catch let error {