SwiftSwift Native Objects (BETA) API Reference for Realtime Apps

 
These docs are for the new PubNub Swift SDK written purely in Swift. To view the docs for the older version (the PubNub Objective-C SDK wrapper for Swift), have a look here.

Creates a user with the specified properties. Returns the created user object, optionally including the user's custom data object.

To Create a User you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userPubNubUserYesThe PubNub user object to create
    includeEnumOptionalnilWhether to include the custom field in the create response
let firstUser = UserObject(name: "Test User", id: "TestUser",
                         custom: ["string": "String", "int": 1, "double": 1.1, "bool": true],
                         created: firstCreatedDate,
                         eTag: "AfuB8q7/s+qCwAE")

pubnub.create(user: firstUser, include: .custom) { result in
  switch result {
  case let .success(payload):
    print("User created: \(payload)")
  case let .failure(error):
    print("Create request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": {
    "id": "user-1",
    "name": "John Doe",
    "externalId": null,
    "profileUrl": null,
    "email": null,
    "created": "2019-08-16T00:03:54.689896Z",
    "updated": "2019-08-16T00:03:54.689896Z",
    "eTag": "Ae+lk5mmuf7pLA"
  }
}

Updates a user with the specified properties. Returns the updated user object, optionally including the user's custom data object.

To Update a User you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userPubNubUserYesThe PubNub user object to update.
    includeEnumOptionalnilWhether to include the custom field in the update response.
let firstUser = UserObject(name: "Test User", id: "TestUser",
                         custom: ["string": "String", "int": 1, "double": 1.1, "bool": true],
                         created: firstCreatedDate,
                         eTag: "AfuB8q7/s+qCwAE")

pubnub.update(user: firstUser) { result in
  switch result {
  case let .success(payload):
    print("Updated user: \(payload)")
  case let .failure(error):
    print("Update request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": {
    "id": "user-1",
    "name": "John Updated Doe",
    "externalId": null,
    "profileUrl": null,
    "email": null,
    "created": "2019-08-16T00:03:54.689896Z",
    "updated": "2019-08-16T00:03:54.689896Z",
    "eTag": "Ae+lk5mmuf7pLA"
  }
}

Deletes the specified user object.

To Delete a User you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userIDStringYesThe PubNub user object to delete.
    includeEnumOptionalnilWhether to include the custom field in the delete response.
pubnub.delete(userID: "TestUser") { result in
  switch result {
  case let .success(payload):
    print("User deleted: \(payload)")
  case let .failure(error):
    print("Delete request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": null
}

Returns a paginated list of user objects, optionally including each user's custom data object.

To Get Users you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
pubnub.fetchUsers(include: .custom) { result in
  switch result {
  case let .success(payload):
    print("Users retrieved: \(payload)")
  case let .failure(error):
    print("Fetch All request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": [
    {
      "id": "user-1",
      "name": "John Doe",
      "externalId": "user-1",
      "profileUrl": "http://profile.com/user/john",
      "email": "john@email.com",
      "created": "2019-08-14T19:12:20.653698Z",
      "updated": "2019-08-15T21:16:12.940425Z",
      "eTag": "Ab+Y676I+7SaKw"
    },
    {
      "id": "user-2",
      "name": "Steve Doe",
      "externalId": "user-2",
      "profileUrl": "http://profile.com/user/steve",
      "email": "steve@email.com",
      "created": "2019-08-15T08:24:10.969884Z",
      "updated": "2019-08-15T14:57:51.747819Z",
      "eTag": "AaLS46PS/tOk6gE"
    }
  ],
  "next": "Mw"
}

Returns the specified user object, optionally including the user's custom data object.

To Get User you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userIDStringYesThe PubNub user object to retrieve.
    includeEnumOptionalnilWhether to include the custom field in the fetch response.
pubnub.fetch(userID: "TestUser") { result in
  switch result {
  case let .success(payload):
    print("User fetched: \(payload)")
  case let .failure(error):
    print("Fetch request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": {
    "id": "user-1",
    "name": "John Doe",
    "externalId": "user-123",
    "profileUrl": "http://profile.com/user/john",
    "email": "john@email.com",
    "created": "2019-08-14T19:12:20.653698Z",
    "updated": "2019-08-15T21:16:12.940425Z",
    "eTag": "Ab+Y676I+7SaKw"
  }
}

Creates a space with the specified properties. Returns the created space object, optionally including the space's custom data object.

To Create a Space you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spacePubNubSpaceYesThe PubNub space object to create
    includeEnumOptionalnilWhether to include the custom field in the create response
let firstSpace = SpaceObject(name: "Test Space", id: "TestSpace",
                           spaceDescription: "Test Description",
                           custom: ["string": "String", "int": 1, "double": 1.1, "bool": true],
                           created: firstCreatedDate,
                           eTag: "AfuB8q7/s+qCwAE")

pubnub.create(space: firstSpace, include: .custom) { result in
  switch result {
  case let .success(payload):
    print("Space created: \(payload)")
  case let .failure(error):
    print("Create request failed with error: \(error.localizedDescription)")
  }
}
{ 
  "status":200,
  "data":{ 
    "id":"space-1",
    "name":"Demo space",
    "description":"a space for the engineering team",
    "created":"2019-08-14T19:11:32.300414Z",
    "updated":"2019-08-14T19:16:14.468003Z",
    "eTag":"Ad7+ocehr5nZHw"
  }
}

Updates a space with the specified properties. Returns the updated space object, optionally including the space's custom data object.

To Update a Space you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spacePubNubSpaceYesThe PubNub space object to update
    includeEnumOptionalnilWhether to include the custom field in the update response
let firstSpace = SpaceObject(name: "Test Space", id: "TestSpace",
                           spaceDescription: "Test Description",
                           custom: ["string": "String", "int": 1, "double": 1.1, "bool": true],
                           created: firstCreatedDate,
                           eTag: "AfuB8q7/s+qCwAE")

pubnub.update(space: firstSpace) { result in
  switch result {
  case let .success(payload):
    print("Space updated: \(payload)")
  case let .failure(error):
    print("Update request failed with error: \(error.localizedDescription)")
  }
}
{ 
  "status":200,
  "data":{ 
    "id":"space-1",
    "name":"Demo Updated Space",
    "description":"a space for the engineering team",
    "created":"2019-08-14T19:11:32.300414Z",
    "updated":"2019-08-14T19:16:14.468003Z",
    "eTag":"Ad7+ocehr5nZHw" 
  }
}

Deletes the specified space object.

To Delete a Space you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spaceIDStringYesThe PubNub space object to delete
    includeEnumOptionalnilWhether to include the custom field in the delete response
pubnub.delete(spaceID: "TestSpace") { result in
  switch result {
  case let .success(payload):
    print("Space deleted: \(payload)")
  case let .failure(error):
    print("Delete request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": null
}

Returns the paginated list of space objects, optionally including the space's custom data object.

To Get Spaces you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of spaces to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the spaces from
    endStringOptionalnilThe end paging hash string to retrieve the spaces from
    countBoolOptionalnilA flag denoting whether to return the total count of spaces
pubnub.fetchSpaces(include: .custom) { result in
  switch result {
  case let .success(payload):
    print("Spaces retrieved: \(payload)")
  case let .failure(error):
    print("Fetch All request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": [
    {
      "id": "space-1",
      "name": "demo space",
      "description": "demo space",
      "created": "2019-08-14T19:10:59.169062Z",
      "updated": "2019-08-14T19:10:59.169062Z",
      "eTag": "AejRtOqosaX9YQ"
    },
    {
      "id": "space-2",
      "name": "main",
      "description": "main space",
      "created": "2019-08-14T19:11:32.300414Z",
      "updated": "2019-08-14T19:16:14.468003Z",
      "eTag": "Ad7+ocehr5nZHw"
    }
  ],
  "next": "Mw"
}

Returns the specified space object, optionally including the space's custom data object.

To Get a Space you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spaceIDStringYesThe PubNub space object to retrieve
    includeEnumOptionalnilWhether to include the custom field in the retrieved response
pubnub.fetch(spaceID: "TestSpace") { result in
  switch result {
  case let .success(payload):
    print("Space retrieved: \(payload)")
  case let .failure(error):
    print("Fetch request failed with error: \(error.localizedDescription)")
  }
}
{ 
  "status":200,
  "data":{ 
    "id":"space-1",
    "name":"Demo space",
    "description":"a space for the engineering team",
    "created":"2019-08-14T19:11:32.300414Z",
    "updated":"2019-08-14T19:16:14.468003Z",
    "eTag":"Ad7+ocehr5nZHw"
  }
}

Get the specified user's space memberships.

To Get Memberships you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userIDStringYesThe user Id to fetch space memberships from
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
pubnub.fetchMemberships(userID: "TestUser") { result in
  switch result {
  case let .success(payload):
    print("Retrieved memberships for user: \(payload)")

  case let .failure(error):
    print("Fetch Memberships request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": [
    {
      "id": "space-1",
      "created": "2019-08-14T19:41:10.31085Z",
      "updated": "2019-08-14T19:56:24.712691Z",
      "eTag": "AYu9+4jfyfXRaQ"
    }
  ],
  "next": "MQ"
}

Get the specified space’s member users.

To Get Members you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spaceIDStringYesThe space Id to fetch members from
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
pubnub.fetchMemberships(spaceID: "TestSpace") { result in
  switch result {
  case let .success(payload):
    print("Retrieved members for space: \(payload)")
  case let .failure(error):
    print("Fetch Memberships request failed with error: \(error.localizedDescription)")
  }
}
{
  "status": 200,
  "data": [
    {
      "id": "user-1",
      "created": "2019-08-14T19:41:10.31085Z",
      "updated": "2019-08-14T19:56:24.712691Z",
      "eTag": "AYu9+4jfyfXRaQ"
    }
  ],
  "next": "MQ"
}

Join a user to a list of spaces.

To Join Spaces you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userIDStringYesThe user Id to fetch space memberships from
    addingArrayOptionalEmpty []The list of spaces to add the user to
    updatingArrayOptionalEmpty []The list of spaces to update for the user
    removingArrayOptionalEmpty []The list of spaces to remove the user from
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
let firstSpace = SpaceObject(name: "First Space",
                           id: "FirstSpace",
                           spaceDescription: "Space Description",
                           created: spaceDate,
                           eTag: "SpaceETag")

pubnub.updateMemberships(userID: "TestUser", adding: [firstSpace]) { result in
  switch result {
  case let .success(payload):
    print("Added user to space: \(payload)")
  case let .failure(error):
    print("Update Memberships request failed with error: \(error.localizedDescription)")
  }
}

Update a user's space memberships.

To Update Memberships you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userIDStringYesThe user Id to fetch space memberships from
    addingArrayOptionalEmpty []The list of spaces to add the user to
    updatingArrayOptionalEmpty []The list of spaces to update for the user
    removingArrayOptionalEmpty []The list of spaces to remove the user from
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
let firstSpace = SpaceObject(name: "First Space",
                           id: "FirstSpace",
                           spaceDescription: "Space Description",
                           eTag: "SpaceETag")

pubnub.updateMemberships(userID: "TestUser", updating: [firstSpace]) { result in
  switch result {
  case let .success(payload):
    print("Added user to space: \(payload)")
  case let .failure(error):
    print("Update Memberships request failed with error: \(error.localizedDescription)")
  }
}

Remove a user from a list of spaces.

To Leave Spaces you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    userIDStringYesThe user Id to fetch space memberships from
    addingArrayOptionalEmpty []The list of spaces to add the user to
    updatingArrayOptionalEmpty []The list of spaces to update for the user
    removingArrayOptionalEmpty []The list of spaces to remove the user from
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
let firstSpace = SpaceObject(name: "First Space",
                           id: "FirstSpace",
                           spaceDescription: "Space Description",
                           eTag: "SpaceETag")

pubnub.updateMemberships(userID: "TestUser", removing: [firstSpace]) { result in
  switch result {
  case let .success(payload):
    print("User removed from space: \(payload)")
  case let .failure(error):
    print("Update Memberships request failed with error: \(error.localizedDescription)")
  }
}

Add a list of users to a space.

To Add Members you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spaceIDStringYesThe space Id to fetch users from
    addingArrayOptionalEmpty []The list of users to add to the space
    updatingArrayOptionalEmpty []The list of users to update for the space
    removingArrayOptionalEmpty []The list of users to remove from the space
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
let firstUser = UserObject(name: "First User",
                         id: "FirstUser",
                         created: spaceDate,
                         eTag: "UserETag")

pubnub.updateMemberships(spaceID: "TestSpace", adding: [firstUser]) { result in
  switch result {
  case let .success(payload):
    print("Added Users to space: \(payload)")
  case let .failure(error):
    print("Update Memberships request failed with error: \(error.localizedDescription)")
  }
}

Update a space's user memberships.

To Update Members you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spaceIDStringYesThe space Id to fetch users from
    addingArrayOptionalEmpty []The list of users to add to the space
    updatingArrayOptionalEmpty []The list of users to update for the space
    removingArrayOptionalEmpty []The list of users to remove from the space
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
let firstUser = UserObject(name: "First User",
                         id: "FirstUser",
                         eTag: "UserETag")

pubnub.updateMemberships(spaceID: "TestSpace", updating: [firstUser]) { result in
  switch result {
  case let .success(payload):
    print("Updated Users on space: \(payload)")
  case let .failure(error):
    print("Update Memberships request failed with error: \(error.localizedDescription)")
  }
}

Remove a list of users from a space.

To Remove Members you can use the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    spaceIDStringYesThe space Id to fetch users from
    addingArrayOptionalEmpty []The list of users to add to the space
    updatingArrayOptionalEmpty []The list of users to update for the space
    removingArrayOptionalEmpty []The list of users to remove from the space
    includeEnumOptionalnilWhether to include the custom field in the fetch response
    limitIntOptionalnilThe number of users to retrieve at a time
    startStringOptionalnilThe start paging hash string to retrieve the users from
    endStringOptionalnilThe end paging hash string to retrieve the users from
    countBoolOptionalnilA flag denoting whether to return the total count of users
let firstUser = UserObject(name: "First User",
                         id: "FirstUser",
                         created: spaceDate,
                         eTag: "UserETag")

pubnub.updateMemberships(spaceID: "TestSpace", removing: [firstUser]) { result in
  switch result {
  case let .success(payload):
    print("Removed User from space: \(payload)")
  case let .failure(error):
    print("Update Memberships request failed with error: \(error.localizedDescription)")
  }
}