惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
月光博客
月光博客
D
DataBreaches.Net
L
LangChain Blog
美团技术团队
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
I
InfoQ
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
腾讯CDC
Martin Fowler
Martin Fowler

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Adding Live Activities to a Pomodoro timer: what the docs...
Yoshiaki Hirokawa · 2026-06-27 · via DEV Community

Yoshiaki Hirokawa

A countdown timer is the perfect use case for Live Activities — and also a great way to discover everything the ActivityKit docs gloss over. When I added Live Activities to Cadento, my SwiftUI Pomodoro app, the "happy path" took an afternoon. The edge cases took a lot longer.

Here's the practical version: what actually matters when you put a focus timer on the lock screen and the Dynamic Island.

The mental model

A Live Activity has three things you define:

  1. ActivityAttributes — the static data, fixed for the lifetime of the activity (e.g. the session's total duration, the task name).
  2. ContentState — the dynamic data that changes over time (e.g. whether it's running or paused).
  3. The SwiftUI views — lock screen, Dynamic Island expanded/compact/minimal.
struct TimerActivityAttributes: ActivityAttributes {
    // static for the whole activity
    let sessionName: String
    let endDate: Date

    struct ContentState: Codable, Hashable {
        // changes over time
        var isPaused: Bool
        var pausedRemaining: TimeInterval?
    }
}

The single biggest "aha" is the next section.

Don't push every second. Let the system count.

The instinct is to update the activity every second so the number ticks down. Don't. You'll hit update throttling and drain battery, and it's unnecessary.

Instead, hand SwiftUI a date range and let it render the countdown itself:

// in the lock screen / Dynamic Island view
Text(timerInterval: context.attributes.startDate...context.attributes.endDate,
     countsDownFrom: 0)
    .monospacedDigit()

Text(timerInterval:) renders a live-updating timer without any activity updates at all. You only push an update when something semantic changes — pause, resume, or finish. That turns "60 updates a minute" into "an update when the user actually does something."

For pause, you stop the auto-counting text and show the frozen remaining time from ContentState.pausedRemaining. That's the whole trick.

The Dynamic Island has four presentations, not one

You must provide all of these, and they're genuinely different layouts:

DynamicIsland {
    // EXPANDED — long-press / when it's the active activity
    DynamicIslandExpandedRegion(.leading)  { /* icon */ }
    DynamicIslandExpandedRegion(.trailing) { /* time  */ }
    DynamicIslandExpandedRegion(.center)   { /* label */ }
    DynamicIslandExpandedRegion(.bottom)   { /* progress */ }
} compactLeading: {
    Image(systemName: "timer")
} compactTrailing: {
    Text(timerInterval: range, countsDownFrom: 0).monospacedDigit()
} minimal: {
    Image(systemName: "timer")
}

Gotchas I hit:

  • Compact trailing has almost no width. A full mm:ss can clip next to other activities. Keep it tight, use .monospacedDigit(), and test with another Live Activity running.
  • Minimal is a single glyph. Don't try to fit text. It shows when multiple activities are active.
  • Tap targets: the whole thing deep-links into your app via widgetURL — wire it up so a tap opens the running timer, not the home screen.

Starting, updating, ending

// start
let activity = try Activity.request(
    attributes: attributes,
    content: .init(state: initialState, staleDate: nil),
    pushType: nil          // local; no push server needed for a timer
)

// update (only on pause/resume/etc.)
await activity.update(.init(state: newState, staleDate: nil))

// end — choose a dismissal policy
await activity.end(.init(state: finalState, staleDate: nil),
                   dismissalPolicy: .after(.now + 4))   // linger 4s, then clear

Notes from shipping it:

  • A timer is localpushType: nil. You do not need a push server. (Push-driven Live Activities are for server-side events like delivery tracking.)
  • dismissalPolicy matters. .immediate yanks it the instant the session ends, which feels abrupt. A short .after(...) lets the user see "Done" before it disappears.
  • Reconnect on launch. When the app comes back, iterate Activity<TimerActivityAttributes>.activities to find and re-attach to an in-flight activity instead of orphaning it.

The things that actually bit me

  • Permission can be off. Users can disable Live Activities globally or per-app. Check ActivityAuthorizationInfo().areActivitiesEnabled and degrade gracefully — don't assume request() will succeed.
  • The widget extension is a separate target. Your Live Activity views live in the widget extension, so shared model code must be in a target both can see, or you'll duplicate types.
  • Background time is not yours. You can't run a precise in-app timer in the background to drive updates. The whole point of Text(timerInterval:) is that the system renders the countdown — lean on it instead of fighting the background execution model.
  • Localize it. Cadento ships in 39 languages; the Live Activity is on-screen text like any other UI, so "Focus" / "Break" / "Paused" all go through the String Catalog too. Easy to forget because it's not in the main app's view tree.

Was it worth it?

For a focus timer, absolutely — it's the feature that makes the app feel present. The session lives on your lock screen and in the Dynamic Island, so you glance and know exactly where you are without opening anything. That's the entire promise of a Pomodoro app, surfaced at the OS level.

The key shift in thinking: you're not driving an animation, you're declaring state and letting the system render time. Once that clicks, Live Activities stop fighting you.


I'm a solo iOS developer from Japan building small, deeply localized apps. Cadento (focus timer, 39 languages, with Live Activities, widgets and an Apple Watch app) is on the App Store. Questions welcome in the comments.