Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219· AnthropicHugging Face LMArena Human Preference 140K

explain how to use ActivityKit to create a live notification on latest iOS

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514’s answer.
  1. User

    explain how to use ActivityKit to create a live notification on latest iOS

  2. Claude Assistant

    Using ActivityKit for Live Notifications on iOS

    To create a live notification on the latest iOS, you'll need to use ActivityKit. Here's how to implement it:

    1. Add the ActivityKit capability to your app in Xcode
    2. Define your Activity attributes struct
    3. Request authorization and start a Live Activity
    4. Update the Live Activity as needed
    5. End the Live Activity when appropriate
    import SwiftUI
    import ActivityKit
    
    // 1. Define your Activity attributes
    struct DeliveryAttributes: ActivityAttributes {
        struct ContentState: Codable, Hashable {
            var status: String
            var estimatedArrival: Date
        }
        
        var orderNumber: String
        var restaurantName: String
    }
    
    // 2. Start a Live Activity
    func startLiveActivity() {
        let initialContentState = DeliveryAttributes.ContentState(
            status: "Preparing",
            estimatedArrival: Date().addingTimeInterval(30 * 60)
        )
        
        let attributes = DeliveryAttributes(
            orderNumber: "ABC123",
            restaurantName: "Pizza Palace"
        )
        
        do {
            let activity = try Activity.request(
                attributes: attributes,
                contentState: initialContentState,
                pushType: .token
            )
            print("Activity started with ID: \(activity.id)")
        } catch {
            print("Error starting activity: \(error)")
        }
    }
    
    // 3. Update a Live Activity
    func updateLiveActivity(activityID: String) {
        let updatedContentState = DeliveryAttributes.ContentState(
            status: "On the way",
            estimatedArrival: Date().addingTimeInterval(15 * 60)
        )
        
        Task {
            for activity in Activity<DeliveryAttributes>.activities {
                if activity.id == activityID {
                    await activity.update(using: updatedContentState)
                }
            }
        }
    }
    
    // 4. End a Live Activity
    func endLiveActivity(activityID: String) {
        Task {
            for activity in Activity<DeliveryAttributes>.activities {
                if activity.id == activityID {
                    await activity.end(
                        using: DeliveryAttributes.ContentState(
                            status: "Delivered",
                            estimatedArrival: Date()
                        ),
                        dismissalPolicy: .immediate
                    )
                }
            }
        }
    }
    

    Would you like me to explain or break down this code?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.