Utilize the User Context API

Accessing some of the API classes that are mentioned on this page requires additional Sentiance SDK dependencies. See this page for more information.

On this page, you can find examples of how to query the Sentiance SDK for the user's current context, and how to register to receive real time updates in your app, as the user's context changes.

Query for the User's Current Context

Sentiance.shared.requestUserContext { context, error in
    guard let context = context else {
        print("Error: \(error?.failureReason)")
        return
    }
    
    print("Recent events:")
    
    // Print the recent events
    context.events.forEach({ event in
        print("Event ID: \(event.eventId)")
        print("Started on: \(event.startDate)")
        print("Ended on: \(event.endDate)")
        
        if event.type == .inTransport {
            let transport =  event as! SENTTransportEvent
            
            print("Type: transport")
            print("Mode: \(toStringMode(transport.transportMode))")
            
            if let distance = transport.distanceInMeters {
                print("Distance: \(distance)")
            }
            
            print("Waypoints: \(transport.waypoints)")
            
        }
        else if event.type == .stationary {
            let stationary =  event as! SENTStationaryEvent
            
            print("Type: stationary")
            print("Location: \(stationary.location)")
            print("Venue: \(stationary.venue)")
        }
        else if event.type == .offTheGrid {
            print("Type: off-the-grid")
        }
        else {
            print("Type: unknown")
        }
        
        print("")
    })
    
    // Print the home & work locations, semantic time, and last known location
    print("Home venue: \(context.home)")
    print("Work venue: \(context.work)")
    print("Semantic time: \(toString(context.semanticTime))")
    print("Last known location: \(context.lastKnownLocation)")
    
    // Print the user's active segments
    print("Active segments:")
    
    context.activeSegments.forEach { segment in
        print("  Category: \(toString(segment.category))")
        print("  Subcategory: \(toString(segment.subcategory))")
        print("  Type: \(toString(segment.type))")
        print("  Start date: \(toString(segment.startDate))")
        print("  End date: \(toString(segment.endDate))")
        
        print("  Attributes: ")
        segment.attributes.forEach { attribute in
            print("    \(attribute.name): \(attribute.value)")
        }
    }
}

Subscribe for User Context Updates

public class UserContextHandler: SENTUserContextDelegate {
    
    public func subscribe() {
        Sentiance.shared.userContextDelegate = self
    }    
        
    public func didUpdate(_ userContext: SENTUserContext, 
       forCriteriaMask criteriaMask: SENTUserContextUpdateCriteria) {
        
        // Check the updated criteria
        if criteriaMask.contains(.currentEvent) {
            print("The user's current event was updated")
        }
        if criteriaMask.contains(.visitedVenues) {
            print("The user's visited venue information was updated")
        }
        if criteriaMask.contains(.activeSegments) {
            print("The user's active segments were updated")
        }
       
        // Handle the updated context data (see the Query for the 
        // User's Current Context example above)
    }
}

Last updated