Service

@objc
public class Service : NSObject

The Service class provides APIs for almost all the workflows supported by SDK. The APIs of this class are used internally by the SDK and what is public can be used by the application.

  • Register the APNS token with Usher Server. The application is expected to register itself with APNS and get the token and then call this service to register the token with Usher Server.

    Declaration

    Swift

    public func updateAPNSToken(token: String, completion: @escaping (UsherErrorProtocol?) -> Void)
    Parameters
    token

    The hex string representation of the APNS token.

  • Delete a badge from user’s another device. If a badge is protected by two-factor authentication e.g phone enrollment then this workflow will trigger phone verificaton in order to prove that the person deleting the badge from other device has control of the phone number associated with that badge. In case where badge does not require two-factor authentication it will be deleted directly from the other device.

    Declaration

    Swift

    public func verifyAndDeleteBadge(deviceId: String, badge: Badge, completion: @escaping (Bool, UsherErrorProtocol?) -> Void)
    Parameters
    deviceId

    The id of the target device from which to delete the badge

    badge

    The id of the badge to be deleted.

  • Fetch and return user’s badges from disk. Can also load latest badge data from the server and write to Realm. Please refer to Realm-Swift documentation at https://realm.io/docs/swift/latest/ if necessary.

    Remark

    Low-level API The backing store for data is Realm. Hence the badge collection returned is a Realm.Results object. If you do not need direct access to realm results please use the APIs provided by BadgeManager

    See also

    BadgeManager

    Declaration

    Swift

    @discardableResult
    public func fetchBadges(cachePolicy: ServiceCachePolicy = .returnCacheDataDontLoad) -> (results: Results<Badge>?, error: UsherErrorProtocol?)
    Parameters
    cachePolicy

    Just return locally present data OR load from the server as well.

    Return Value

    A tuple of realm results & error. Realm results are live and maintain an active connection to the database and will auto update if the underlying realm change. It is not safe to share any realm related objects across threads. Realm objects should be consumed on the thread they were created.

  • Fetch and return user’s badges from disk. Can also load latest badge data from the server and write to Realm. The completion block does not provide the latest badge data since realm results are auto-updating. Please refer to Realm-Swift documentation at https://realm.io/docs/swift/latest/ if necessary.

    Remark

    Low-level API The backing store for data is Realm. Hence the badge collection returned is a Realm.Results object. If you do not need direct access to realm results please use the APIs provided by BadgeManager

    See also

    BadgeManager

    Declaration

    Swift

    @discardableResult
    public func fetchBadges(cachePolicy: ServiceCachePolicy = .returnCacheDataDontLoad, completion: @escaping (([UsherErrorProtocol]) -> Void)) -> (results: Results<Badge>?, error: UsherErrorProtocol?)
    Parameters
    cachePolicy

    Just return locally present data OR load from the server as well.

    Return Value

    A tuple of realm results & error. Realm results are live and maintain an active connection to the database and will auto update if the underlying realm change. It is not safe to share any realm related objects across threads. Realm objects should be consumed on the thread they were created.

  • Fetch the latest data for the specified badge.

    Remark

    Low-level API You could use BadgeManager instead
    Declaration

    Swift

    public func reloadBadge(_ badge: Badge, completion: (()->Void)? = nil)
    Parameters
    badge

    The badge for which to fetch latest data.

    Return Value

    Void. The assumption is that the caller already holds reference to a live realm results which will be auto updated when this call fetches latest data.

  • Returns a live badge object for the given badge ID.

    Remark

    Low-level API You could use BadgeManager instead
    Declaration

    Swift

    public func badgeForId(_ badgeId: String?) -> Badge?
    Parameters
    badgeId

    The id of the badge to be returned.

    Return Value

    Realm-managed(live) Badge object if present.

  • Returns a live badge object for the given organization id.

    Remark

    Low-level API You could use BadgeManager instead.
    Declaration

    Swift

    public func badgeForOrgId(_ orgId: String, server: Server? = nil) -> Badge?
    Parameters
    orgId

    The id of the organization whose badge to be returned. Only the first badge matching the organization Id will be returned.

    Return Value

    Realm-managed(live) Badge object if present.

  • Fetch the user profile image for the badge. This only applies to the default badge design as setup on network manager. If implementing custom badge design then calling this API may be unnecessary. UsherSDK does maintain a basic image cache to prevent redundant calls to get the same image.

    Declaration

    Swift

    @objc
    public func fetchProfileImageDataFor(badgeId: String, completion: @escaping (_ badgeId: String, _ imageData: Data?) -> Void)
    Parameters
    badge

    The badge for which to fetch the background image.

    Return Value

    Tuple of badge ID & the image data for the user profile image.

  • Fetch the icon image for the badge. This only applies to the default badge design as setup on network manager. If implementing custom badge design then calling this API may be unnecessary. UsherSDK does maintain a basic image cache to prevent redundant calls to get the same image.

    Declaration

    Swift

    public func fetchIconForBadge(_ badge: Badge, completion: @escaping (_ badgeId: String, _ image: UIImage?) -> Void)
    Parameters
    badge

    The badge for which to fetch the background image.

    Return Value

    Tuple of badge ID & the image for the badge icon.

  • Fetch the icon image for the badge. This only applies to the default badge design as setup on network manager. If implementing custom badge design then calling this API may be unnecessary. UsherSDK does maintain a basic image cache to prevent redundant calls to get the same image.

    Declaration

    Swift

    public func fetchIconDataForBadge(_ badge: Badge, completion: @escaping (_ badgeId: String, _ imageData: Data?) -> Void)
    Parameters
    badge

    The badge for which to fetch the background image.

    Return Value

    Tuple of badge ID & the image data for the badge icon.

  • Fetch the background image for the badge. This only applies to the default badge design as setup on network manager. If implementing custom badge design then calling this API may be unnecessary. UsherSDK does maintain a basic image cache to prevent redundant calls to get the same image.

    Declaration

    Swift

    public func fetchBackgroundImageForBadge(_ badge: Badge, completion: @escaping (_ badgeId: String, _ imageData: Data?) -> Void)
    Parameters
    badge

    The badge for which to fetch the background image.

    Return Value

    Tuple of badge ID & the image data for the badge background.

  • Update the user profile image for the badge. Please make sure that the image being uploaded adheres to the acceptable profile image rules of you organization.

    Declaration

    Swift

    public func updateUserProfileImageForBadge(_ badge: Badge, image: Data, completion: @escaping (UsherErrorProtocol?) -> Void)
    Parameters
    badge

    Badge for which to update user profile image.

  • Request the server to send a badge recovery code for the specified email address.

    Seealso

    UsherErrorProtocol
    Declaration

    Swift

    public func requestRecoveryCodeForEmail(_ email: String,
                                            orgId: String? = nil,
                                            completion: @escaping (UsherErrorProtocol?) -> Void)
    Parameters
    email

    User’s email address for which an user account exists on Usher Server.

    orgId

    Specify this if the recovery code generated should be just for badge(s) of the specified organization.

  • Boolean representing whether the current server supports the Inbox feature or not

    Declaration

    Swift

    public var serverSupportsInbox: Bool { get }
  • Load items from inbox with the specified MessageType and list of badge ids.

    See also

    MessageType

    Declaration

    Swift

    public func loadItemsFromInbox(type messageType: MessageType,
                                   badgeIds: [String],
                                   completion: @escaping ([InboxMessage]?, UsherErrorProtocol?) -> Void)
    Parameters
    type

    The type of the inbox message to load

    badgeIds

    The id of the badges for which to load the inbox items.

    forceRefresh

    Should the latest data be fetched from server.

  • Load all read, replied & unread items from inbox for the specified badgeIds.

    Declaration

    Swift

    public func loadAllItemsFromInbox(badgeIds: [String],
                                      completion: @escaping ([InboxMessage]?, UsherErrorProtocol?) -> Void)
    Parameters
    badgeIds

    The id of the badges for which to load the inbox items.

  • Gets all cached messages

    Declaration

    Swift

    public func cachedMessages() -> Results<InboxMessage>?
    Parameters
    badgeIds

    Badge IDs

    Return Value

    List of all messages: Unread and replied

  • Get cached inbox messages

    Declaration

    Swift

    public func cachedMessages(badgeIds: [String], type: MessageType) -> Results<InboxMessage>?
    Parameters
    badgeIds

    Badge IDs

    type

    Message type

    Return Value

    Realm Result of cached messages

  • Fetch the last modification time of messages from server. This method is long running, so get the timestamp from the completion block

    Declaration

    Swift

    public func loadLastModifiedTime(badgeIds: [String], completion: @escaping (TimeInterval?) -> Void)
    Parameters
    badgeIds

    Badge Ids of messages to check modification time for.

    completion

    Completion block called after server call is complete with an optional time stamp if server fetch was successful.

  • Reply to an inbox message

    Declaration

    Swift

    public func replyToMessage(_ message: InboxMessage,
                               replyIndex: Int,
                               completion: ((Bool, UsherErrorProtocol?) -> Void)?)
    Parameters
    message

    The message thats being replied to

    replyIndex

    The index of the selected reply option.

    completion

    Completion block called after message is sent. The completion block have two arguments for whether the call was successful and if any error occured while replying.

  • Remove a message from local cache. This method is only useful after replying to a message. In any other case than that, the message will be recreated locally when fetching messages from server.

    Declaration

    Swift

    public func removeMessageFromLocalCache(_ message: InboxMessage)
    Parameters
    message

    The message to remove.

  • Unmark the key as a favorite key.

    Remark

    Low-level API
    Declaration

    Swift

    public func deleteFavoriteKey(_ keyContainer: KeyContainer, serverSync: Bool = true)
    Parameters
    serverSync

    Save the changes to server.

  • Fetch and return all or favorite keys for a badge. CAn also load latest keys from server and write to Realm Please refer to Realm-Swift documentation at https://realm.io/docs/swift/latest/ if necessary.

    Remark

    Low-Level API. Use KeysInteractor instead. The backing store for data is Realm. Hence the keys collection returned is a Realm.Results object. If you do not need direct access to realm results please use the APIs provided by KeysInteractor

    Declaration

    Swift

    @discardableResult
    func fetchKeysForBadge(_ badge: Badge, favorite: Bool = false) -> (results: Results<KeyContainer>?, error: UsherErrorProtocol?)
    Parameters
    badge

    The badge to fetch keys for.

    favorite

    Return user’s favorite keys for the badge or all keys for the badge.

    Return Value

    A tuple of realm results & error. Realm results are live and maintain an active connection to the database and will auto update if the underlying realm change. It is not safe to share any realm related objects across threads. Realm objects should be consumed on the thread they were created.

  • Mark a key as favorite key for the user.

    Remark

    Low-Level API Use KeysInteractor instead
    Declaration

    Swift

    func addFavoriteKey(_ keyContainer: KeyContainer, serverSync: Bool = true)
    Parameters
    serverSync

    Sync the favorite keys to server.

  • Reorder user’s favorite keys for a badge. Reordering works only within the favorite key collection for a badge and not across badges.

    Remark

    Low-Level API Use Keys interactor instead.
    Declaration

    Swift

    func moveFavoriteKey(_ keyContainer: KeyContainer, from sourceIndex: Int, to destinationIndex: Int)
  • Use a key the user has access to. The workflow will proceed to check the security restrictions for the associated badge and attempt to process them before processing the use key request.

    Remark

    Low-Level API Use KeysInteractor instead

    Declaration

    Swift

    func useKey(_ keyContainer: KeyContainer,
                keyIndex: Int,
                _ completion: @escaping (_ success: Bool, _ error: UsherErrorProtocol?) -> Void)
    Parameters
    keyIndex

    Index of the key in the KeyContainer object to use.

    Return Value

    Tuple of boolean indicating success / failure and error.

  • Use a key the user has access to. The workflow will proceed to check the security restrictions for the associated badge and attempt to process them before processing the use key request.

    Remark

    Low-Level API Use KeysInteractor instead

    Declaration

    Swift

    func useKey(_ keyContainer: KeyContainer,
                keyId: String,
                _ completion: @escaping (_ success: Bool, _ error: UsherErrorProtocol?) -> Void)
    Parameters
    keyId

    Id of the key to use.

    Return Value

    Tuple of boolean indicating success / failure and error.

  • Upload the local favorite keys state to server.

    Remark

    Low-Level API
    Declaration

    Swift

    func syncFavoriteKeys(badgeId: String, addedLocally: Set<KeyContainer>, removedLocally: Set<KeyContainer>)
  • Get a list of badges that are pending Multi Factor Authentication enrollment or verification.

    Declaration

    Swift

    public func badgesPendingMFA() -> Results<Badge>?
    Return Value

    Collection of type Realm.Results

  • Record the scan of codes other than Usher Resource Codes for every badge that is configured to record such scans. The scan will be recorded only for badges that do not have any unsatisfied security restrictions.

    Declaration

    Swift

    func recordGenericCodeScan(_ code: String,
                               type: CodeType,
                               _ completion: @escaping ( _ code: String, _ badges: Results<Badge>?, _ results: [String: RecordScanResult]) -> Void)
    Parameters
    code

    The string representation of the scanned code.

    type

    The type of scanned code as string

    completion

    Provides a dictionary of badge ID to result of record scan.

  • Fetch the history of codes scanned by the user.

    Remark

    This API may change in future to abstract away the Realm data types.

    Declaration

    Swift

    public func loadScannedActions() -> Results<ScanLogEntry>?
    Return Value

    Collection object of type Realm.Results. Please refer to Realm-Swift documentation at https://realm.io/docs/swift/latest/ if necessary.

  • Clear the history of scanned codes for this device.

    Declaration

    Swift

    public func clearAllLogEntries(completion: (()->Void)? = nil)
  • This is an UsherPro specific API and is used to reply to multiple choice questions pushed by Usher administrators to their users using the Usher app.

    See also

    ReplyToChatRequest
    Declaration

    Swift

    public func replyToChat(request: ReplyToChatRequest, completion: @escaping (NSError?) -> Void)
  • Validate another user by their Usher QR code or numeric Usher Code.

    Remarks

    Use ScanProcessor if possible.

    Declaration

    Swift

    public func verifyUsherCode(code: String, orgId: String, forBadgeId badgeId: String? = nil, completion: @escaping (Badge?, UsherErrorProtocol?) -> Void)
    Parameters
    code

    Usher QR code OR Usher numeric code

    orgId

    The organization id of the verifying badge. The workflow will proceed if the verifying user (this device) has a badge with this organization id.

    Return Value

    Tuple of badge & error. If user verification succeeds the returned badge is the badge of the verified user.