UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Extended runtime for iOS or advice on silent meditation app

Forums > SwiftUI

I'm creating a silent meditation app in swift and unclear on the recommended approach for alerting the user with a sound that their session has ended. The issue being that over the course of a longer meditation session, the app will likely go into the background.

This exact scenario is solved for on watchOS with extended runtime sessions. But similar APIs don't seem available for iOS.

Meditation sessions could be 30+ minutes, which I understand will exceed any time the system will allow for any background tasks.

The only audio in the app is the brief sound effect played at the start and end of the meditation session. And since the app will stop running code once it goes into the background, I'm unclear on how I would play the sound effect at the end.

I want to avoid using a local notification to alert them their session is complete because if their ringer is set to silent they wouldn't hear the notification. And it would be awkward to ask them to agree to receiving notifications just so they can know when their session has ended.

Advice on the recommended approach for solving this problem would be greatly appreciated!

3      

Rather than thinking in terms of a single timer that lasts 30 mins, think of it as a single timer that runs for one minute and repeats until 30 mins have passed.

As an example, here is a short timer loop that runs every 1 second until 30 seconds hava passed. You can extrapolate from this to make it fit your needs.

class MeditationClock {

    var meditationTimer: Timer = Timer()
    var endMeditationTime: Date = Date()
    var meditationInProgress: Bool = false

    var currentTime: Date {
        Date()
    }

    init() {
        endMeditationTime = Calendar.current.date(byAdding: .second, value: 30, to: Date())!
        meditationInProgress = true
        meditationTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(checkTimer), userInfo: nil, repeats: true)
    }

    @objc func checkTimer() {

        guard meditationInProgress == true else {
            print("User ended meditation practice...")
            meditationTimer.invalidate()
            return
        }

        if currentTime > endMeditationTime {
            print("Meditaton Complete")
            meditationInProgress = false
            meditationTimer.invalidate()
            return
        }

        print("Meditating: Breath...")
    }
}

To run it, just initialize the MeditationClock object.

let meditationClock: MeditationClock = MeditationClock()

3      

In the 100 days of swiftui course on here (its free) there is an app called HotProspects that you write. In this app you set up a notification that can give you a message at a particular time of day. This sounds like it will do what you need.

3      

Another option may be...to prevent the app from going idle with this:

UIApplication.shared.isIdleTimerDisabled = true

Now you can set your timer to whatever length you like, maybe play some nature sounds in the background and you're all set.

3      

Hi @TheTwoNotes, thanks so much for your thoughtful responses.

My issue isn't with building a timer, it's that the timer will stop running when the app goes into the background. This will happen either because of the auto-lock setting on their phone, or because the user does so unintentionally e.g. I've observed that some people out of habit will lock their phone as they set it down.

My current solution is it disable the idle timer, as you suggest. But I'm trying to understand what the more durable solution would be.

3      

Hi @MrGLWatston, thanks for the tip.

I want to avoid using a local notification to alert them their session is complete because if their ringer is set to silent they wouldn't hear the notification. And it would be awkward to ask them to agree to receiving notifications just so they can know when their session has ended.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.